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;
本文介绍如何在Delphi应用中使用TWebBrowser控件执行自定义的JavaScript或VBScript脚本。通过提供的代码示例,展示了如何定位HTML文档中的元素并显示其标签名称。

被折叠的 条评论
为什么被折叠?



