Solved

click button and run onmousedown event

Posted on 2011-09-30
8
1,474 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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 500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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