蓖麻油 迷迭香增长头发:如何用Delphi在TWebBrowser中調用JavaScript
来源:百度文库 编辑:九乡新闻网 时间:2024/05/05 11:42:30
在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在TWebBrowser中調用JavaScript
如何用FIDDLER2直接调试线上页面的JavaScript和CSS
在PS中如何用通道调色?|经典教程|
在PS中如何用通道调色?|经典教程|000
如何用VB在窗体中实现闪烁文字3
在谈判中如何用英语表露不满
如何用建行信用卡在淘宝上消费
如何用建行信用卡在淘宝上消费0
如何用图片做底板在图片上写日记
如何用数字万用表在电路板中测量电解电容
如何用蓝牙耳机在电脑上听歌和语音聊天
如何用蓝牙耳机在电脑上听歌和语音聊天
如何用图片做底板在图片上写日记?
delphi
如何用按摩治疗
如何用电脑看电视
如何用Adsense挣钱
如何用歇后语骂人
如何用“小人”?
如何用太极拳打人
PPT模板如何用
【如何用电脑看电视】
如何用酸奶去死皮
【引用】如何用运掌中符