在网页的.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;
即可得到运行结果: