Delphi在TWebBrowser中调用JavaScript

How to call JavaScript functions in a TWebBrowser from Delphi

如何用Delphi在TWebBrowser中调用JavaScript
Rewritten by tamsun
Source From delphidabbler.com

方法介绍

在TWebBrowser中调用脚本的办法是调用Html文档相关的对象窗口中的execScript方法。至于什么是和Html Document相关的对象窗口,后面的代码中用到的IHTMLWindow2就是。execScript函数定义如下:

function execScript(const code: WideString; const language: WideString): OleVariant;

参数code是一个脚本函数的完整调用形式的字符串,例如有一个JavaScript函数定义为:
function foo(param1),则 code=”foo(param1)”。

参数language表示脚本的类型,例如 language=”JavaScript”

首先,获取浏览器组件的文档对象;然后通过该文档对象的ParentWindow属性来获取窗口对象。最后通过该窗口对象来调用execScript即可。下面就给出一个简单的实现示例。

实现示例

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;

实例演示

整个实例包括两部分:

网页文件test.html:文件内有一个JavaScript函数SetFont。该函数通过下拉框来选择字体,然后点击”set font“按钮来改变页面字体。
Delphi端程序:通过TWebbrowser来显示页面,并演示如何调用页面内的Javascript函数。

Test.html:



Demo for call Javascript from Delphi<br />


demo of calling Javascript from Delphi




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.

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

28 − 26 =