来安永阳社区居委会:如何用Delphi在TWebBrowser中调用JavaScript
来源:百度文库 编辑:中财网 时间:2024/04/28 12:59:06
在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
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.
补充材料
上述Delphi代码界面截图:
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 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.
补充材料
上述Delphi代码界面截图:
如何用delphi实现?
如何用delphi使某一窗口显示在最前面
如何用Delphi实现MD5?
如何用Delphi实现MD5?
如何用delphi实现另存为?
如何用delphi制作屏保
如何用DELPHI动态生成ACCESS数据库?
如何用DELPHI压缩ACCESS数据库?
如何用delphi制作一个播放器
如何用Delphi控制MySQL数据库
在DELPHI中用QREPORT制作报表,如何用QIMAGE连续输出JPEG图片
如何用delphi在其他程序窗口内生成虚拟鼠标,与实际鼠标互不相干?
如何用delphi在其他程序窗口内生成虚拟鼠标,与实际鼠标互不相干?
delphi中如何用默认浏览器打开一个网址?
如何用delphi构造树型菜单(在线等)
如何用DELPHI语言编写IP地址的自动获取
请问如何用Access读取Delphi的数据库
delphi如何用sql 语句动态建本地数据库表
如何用DELPHI做程序升级的接口(急)
如何用delphi设计程序打开dxf图像
请教:如何用delphi编程实现语音文件的合并
请问,如何用delphi进行GDI的开发?
如何用ADO连接前台DELPHI与数据库
如何用delphi创建accsee数据库,如何建一个表。