如何在Delphi中调用网页中的JavaScript

在网页的.js文件中增加函数: 

function alertClientMessage(cmsg){ 
    alert('提示信息'+cmsg); 
}

在Delphi中加入一个WebBrowser,并加载此网页。执行下面的代码即可调用网页中的JavaScript。 

procedure TForm1.btn2Click(Sender: TObject); 
var 
   LDocument: IHTMLDocument2;
begin 
  wb1.Document.QueryInterface(IHTMLDocument2,LDocument); 

  if not Assigned(LDocument) then Exit;  
 
  LDocument.parentWindow.execScript('alertClientMessage("AB==CDEFG")','JavaScript'); 
end; 

即可得到运行结果:

posted @ 2013-04-02 16:46  努力CODE  阅读(601)  评论(0)    收藏  举报