How to execute a javascript code on a twebbrowser document
The twebbrowser Delphi control provides access to the web browser functionality from your Delphi applications.
Here's how to execute a custom script (JavaScript or VBScript) function on a HTML document loaded in the twebbrowser control:
Uses mshtml_tlb, shdocvw;
Procedure executescript (DOC: ihtmldocument2; Script: string; language: string );
Begin
If doc <> nil then
Begin
If Doc. parentwindow <> nil then
Doc. parentwindow. execScript (script, olevariant (language ));
End;
End;
Usage (in some button onclick event handler, for example ):
VaR
Script: string;
Begin
// Locate the first element with ID attribute = "Main" and show its tag
Script: = 'var elemmain = Document. getelementbyid ("Main"); If (elemmain! = NULL) {alert (elemmain. tagname );}';
Executescript (embeddedwb1.document as ihtmldocument2, script, 'javascript ')
End;