Solved

click button and run onmousedown event

Posted on 2011-09-30
8
1,467 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Internet Explorer View Settings Question 15 111
Help on project with Soap 10 54
Delphi application Soap connection 5 103
Controlled Assessment GCSE - desperate help needed 4 84
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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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