Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

click button and run onmousedown event

Posted on 2011-09-30
8
Medium Priority
?
1,543 Views
Last Modified: 2012-05-12
hi i have question

<a onmousedown="alert('hello world');"  href="sample.html"> Hello world </a>

how can i call to onmousedown event with button click on delphi form ?


sory my poor english.
0
Comment
Question by:gsamuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:inaira
ID: 36895504
Click your button, see Object Inspector, click Events tab, double click OnMouseDown then write your code there.
0
 
LVL 3

Expert Comment

by:Morphor
ID: 36895781
You need just the alert('hello world') function right?

uses mshtml;
...
procedure TForm1.CallAlert(S: string);
  { Calls JavaScript alert() function }
var
  Doc: IHTMLDocument2;      // current HTML document
  HTMLWindow: IHTMLWindow2; // parent window of current HTML document
  JSFn: string;             // stores JavaScipt function call
begin
  // Get reference to current document
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  // Get parent window of current document
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  // Run JavaScript
  try
    JSFn := Format('alert("%s")', [S]);  // build function call
    HTMLWindow.execScript(JSFn, 'JavaScript'); // execute function
  except
    // handle exception in case JavaScript fails to run
  end;
end;

TForm1.Button1Click(Sender: TObject);
begin
  CallAlert('hello world');
end;

Open in new window

0
 

Author Comment

by:gsamuk
ID: 36895967
No Morphor , any function, hello word only sample  
i want to activated any onxxx   functions,  (onmouseover, onclick, onmousedown) my project will searching any  element on document and activated, element onxxx event ...

 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:Morphor
ID: 36895994
In that case it'd be good if the tags had ids, for example:

<a onmousedown="alert('hello world');" id="5" href="sample.html">

Open in new window


Is that possible?
0
 

Author Comment

by:gsamuk
ID: 36896278
yes it'is possible...
0
 
LVL 3

Accepted Solution

by:
Morphor earned 2000 total points
ID: 36896746
Modified the code I posted in the last thread:

Tell me if you need any help but I think it's pretty obvious how it is to be used. Pay attention to the last procedure - Button1Click - and check my upper post.

uses mshtml;
//.........TForm PUBLIC DECLARATION
public
    function GetElementIdValue(WebBrowser: TWebBrowser; TagName, TagId, TagAttrib: string): string;
    procedure CallMethod(S: string);
//..........IMPLEMENTATION
function extractMethod(S: String): String;
var mBegin, mEnd, i: integer;
begin
  Result := '';
  mBegin := pos('{', S);
  mEnd := pos('}', S);
  Result := Copy(S, mBegin, mEnd);
end;

function TForm1.GetElementIdValue(WebBrowser: TWebBrowser; TagName, TagId, TagAttrib: string):string;
var
  Document: IHTMLDocument2;
  Body: IHTMLElement2;
  Tags: IHTMLElementCollection;
  Tag: IHTMLElement;
  I: Integer;
begin
  Result:='';
  if not Supports(WebBrowser.Document, IHTMLDocument2, Document) then
    raise Exception.Create('Invalid HTML document');
  if not Supports(Document.body, IHTMLElement2, Body) then
    raise Exception.Create('Can''t find <body> element');
  Tags := Body.getElementsByTagName(UpperCase(TagName));
  for I := 0 to Pred(Tags.length) do begin
    Tag:=Tags.item(I, EmptyParam) as IHTMLElement;
    if Tag.id=TagId then Result := Tag.getAttribute(TagAttrib, 0);
  end;
end;

procedure TForm1.CallMethod(S: string);
var
  Doc: IHTMLDocument2;      // current HTML document
  HTMLWindow: IHTMLWindow2; // parent window of current HTML document
begin
  // Get reference to current document
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  // Get parent window of current document
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  // Run JavaScript
  try
    HTMLWindow.execScript(S, 'JavaScript'); // execute function
  except
    // handle exception in case JavaScript fails to run
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var x: String;
begin
  x := extractMethod(getElementIDValue(WebBrowser1, 'a', '5', 'onmousedown'));
  if x <> '' then CallMethod(x) else showMessage('No Method Found.');
end;

Open in new window

0
 

Author Comment

by:gsamuk
ID: 36897360
than you morphor, this functions  like i want,  
but give errorr  "EOleException, Error 80020101"
how can i do for this error ?
0
 
LVL 3

Expert Comment

by:Morphor
ID: 36897533
Can you post the source code you are testing it with? I mean the html file.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question