WebBrowser执行脚本和调用外部方法。
2008-10-06 09:54
721 查看
控制WebBrowser实际上就是控制IE,最简单的方法就是执行javascript或vbscript,省去了接口的转换。
如何执行脚本?以前我一直用mshtml中IHTMLWindow2接口的execScript()方法,在Delphi中需要uses MSHTML单元:
uses MSHTML;
procedure TForm1.Button1Click(Sender: TObject);
begin
(WebBrowser1.Document as IHTMLDocument2).parentWindow.execScript(
'alert("hello");', 'javascript')
end;
在CSharp中则需要在工程添加Micrsoft.mshtml,后来得到在地址栏执行脚本的启发。用WebBrowser的Navigate()方法更简单:
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('javascript:alert("hello");')
end;
省去了添加引用的麻烦。
如何调用外部的方法?先看一段在IE中添加收藏夹的代码:window.external.AddFavorite(url, title);
脚本中window.external对象就是一个外部对象,AddFavorite()则是这个外部对象的方法!
查了一下资料,原来可以通过IDocHostUIHandler接口的GetExternal()方法,指定脚本的外部对象。
在CSharp中更简单,有WebBrowser.ObjectForScripting属性直接对应window.external,参考如下代码:
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = @"
<html>
<input type=""button"" value=""测试"" onclick=""alert('Zswang 路过');"">
</html>
";
}
public void alertMessage(string s)
{
MessageBox.Show(s, "囧");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate(@"javascript:
function alert(str)
{
window.external.alertMessage(str);
}");
webBrowser1.ObjectForScripting = this;
}
}
如何执行脚本?以前我一直用mshtml中IHTMLWindow2接口的execScript()方法,在Delphi中需要uses MSHTML单元:
uses MSHTML;
procedure TForm1.Button1Click(Sender: TObject);
begin
(WebBrowser1.Document as IHTMLDocument2).parentWindow.execScript(
'alert("hello");', 'javascript')
end;
在CSharp中则需要在工程添加Micrsoft.mshtml,后来得到在地址栏执行脚本的启发。用WebBrowser的Navigate()方法更简单:
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('javascript:alert("hello");')
end;
省去了添加引用的麻烦。
如何调用外部的方法?先看一段在IE中添加收藏夹的代码:window.external.AddFavorite(url, title);
脚本中window.external对象就是一个外部对象,AddFavorite()则是这个外部对象的方法!
查了一下资料,原来可以通过IDocHostUIHandler接口的GetExternal()方法,指定脚本的外部对象。
在CSharp中更简单,有WebBrowser.ObjectForScripting属性直接对应window.external,参考如下代码:
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = @"
<html>
<input type=""button"" value=""测试"" onclick=""alert('Zswang 路过');"">
</html>
";
}
public void alertMessage(string s)
{
MessageBox.Show(s, "囧");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate(@"javascript:
function alert(str)
{
window.external.alertMessage(str);
}");
webBrowser1.ObjectForScripting = this;
}
}
相关文章推荐
- WebBrowser执行脚本和调用外部方法
- WebBrowser执行脚本和调用外部方法
- 调用JOB执行外部脚本报错ORA-27369解决方法
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法
- C#调用执行外部程序的实现方法
- Matlab程序调用外部exe(可执行文件)方法及参数传递
- 在Java中执行脚本命令以及调用外部程序说明
- shell脚本手动执行成功,定时任务调用失败的解决方法。
- java 执行命令、调用bat文件、调用外部程序的两种方法
- JAVA学习22_在Java中执行脚本命令以及调用外部程序说明
- 使 WebBrowser 更简单的插入、调用执行脚本, 可安装 jQuery 脚本的 C# 开源代码 - IEBrowser
- [Android]Recovery调用外部Shell脚本,Shell脚本使用ui_print方法
- 使 WebBrowser 更简单的插入、调用执行脚本, 可安装 jQuery 脚本的 C# 开源代码 - IEBrowser
- WebView中调用assets下的js脚本无法正常执行的解决方法
- 通过WebBrowser中的脚本调用Delphi自定义的方法(How to call Delphi code from scripts running in a TWebBrowser)
- Linq排序、分组、模糊查询、调用外部方法、直接执行SQL语句、事务、修改数据
- winform c#调用外部EXE程序,并等待外部EXE程序执行完毕才执行下面的代码的方法。
- C++中调用外部可执行程序的方法--创建子进程
- WebBrowser例程 - 插入HTML和执行脚本的方法
- 在程序中调用外部程序,用process返回命令执行结果以及抓取错误信息的方法