分享

如何用Delphi在TWebBrowser中调用JavaScript

 达能牛牛 2007-04-05
在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: 


<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.
 
 补充材料   
 
上述Delphi代码界面截图:

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多