How to call JavaScript functions in a TWebBrowser from Delphi
如何用Delphi在TWebBrowser中調(diào)用JavaScript
Rewritten by tamsun
Source From delphidabbler.com
方法介紹
在TWebBrowser中調(diào)用腳本的辦法是調(diào)用Html文檔相關(guān)的對(duì)象窗口中的execScript方法。至于什么是和Html Document相關(guān)的對(duì)象窗口,后面的代碼中用到的IHTMLWindow2就是。execScript函數(shù)定義如下:
function execScript(const code: WideString; const language: WideString): OleVariant;
參數(shù)code是一個(gè)腳本函數(shù)的完整調(diào)用形式的字符串,例如有一個(gè)JavaScript函數(shù)定義為:
function foo(param1),則 code="foo(param1)"。
參數(shù)language表示腳本的類型,例如 language="JavaScript"
首先,獲取瀏覽器組件的文檔對(duì)象;然后通過(guò)該文檔對(duì)象的ParentWindow屬性來(lái)獲取窗口對(duì)象。最后通過(guò)該窗口對(duì)象來(lái)調(diào)用execScript即可。下面就給出一個(gè)簡(jiǎn)單的實(shí)現(xiàn)示例。
實(shí)現(xiàn)示例
uses
MSHTML;
procedure TForm1.CallFoo(S: string; I: Integer);
{ Calls JavaScript Foo() 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(‘Foo(‘‘%s‘‘,%d)‘, [S, I]); // build function call
HTMLWindow.execScript(JSFn, ‘JavaScript‘); // execute function
except
// handle exception in case JavaScript fails to run
end;
end;
實(shí)例演示
整個(gè)實(shí)例包括兩部分:
網(wǎng)頁(yè)文件test.html:文件內(nèi)有一個(gè)JavaScript函數(shù)SetFont。該函數(shù)通過(guò)下拉框來(lái)選擇字體,然后點(diǎn)擊”set font“按鈕來(lái)改變頁(yè)面字體。
Delphi端程序:通過(guò)TWebbrowser來(lái)顯示頁(yè)面,并演示如何調(diào)用頁(yè)面內(nèi)的Javascript函數(shù)。
Test.html:
<html>
<head>
<title> Demo for call Javascript from Delphi
</title>
<script type="text/javascript">
<!--
function SetFont(fontname)
{
document.body.style.fontFamily = fontname;
}
-->
</script>
</head>
<body>
demo of calling Javascript from Delphi
<form>
<select size=1 name="selfont">
<option value="Verdana" selected>Verdana</option>
<option value="Arial">Arial</option>
<option value="Courier New">Courier New</option>
<option value="Tahoma">Tahoma</option>
</select>
<input type="button" value="set font" name="btn1"
onclick="SetFont(selfont.value)">
</form>
</body>
</html>
Delphi控制Javascript
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, Mshtml;
type
TForm1 = class(TForm)
btnCallJS: TButton;
cmbFonts: TComboBox;
WebNav: TWebBrowser;
procedure FormShow(Sender: TObject);
procedure WebNavDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure btnCallJSClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
// Disable button
btnCallJS.Enabled := false;
// Load the Html page
WebNav.Navigate(ExtractFilepath(Application.ExeName)
+‘test.html‘);
end;
procedure TForm1.WebNavDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
// When complete loading Html page, enable button
btnCallJS.Enabled := true;
end;
// Call the Javascript in Html page
procedure TForm1.btnCallJSClick(Sender: TObject);
var
// current Html document
Doc : IHtmlDocument2;
// parent window of current Html document
HtmlWnd : IHtmlWindow2;
// Javascript function name including arguments
JsFnc : string;
begin
// Get reference to current document
Doc := WebNav.Document as IHtmlDocument2;
if not assigned(Doc) then
exit;
// Get parent window of current Html document
HtmlWnd := Doc.parentWindow;
if not assigned(HtmlWnd) then
exit;
// Run Javascript
try
JsFnc := ‘SetFont(‘‘‘ + trim(cmbFonts.Text) + ‘‘‘)‘;
HtmlWnd.execScript(JsFnc, ‘JavaScript‘);
except
Showmessage(‘Call JavaScript failed!‘);
end;
end;
end.