Solved

click button and run onmousedown event

Posted on 2011-09-30
8
1,455 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 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

17 Experts available now in Live!

Get 1:1 Help Now