Solved

click button and run onmousedown event

Posted on 2011-09-30
8
1,444 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now