1.引用using mshtml; //Microsoft HTML Object Library 2.[ComVisible(true)] //放到类的上边 public partial class Form1 : Form 3.//激活webBrowser1_Navigated事件 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEve
1.引用using mshtml; //Microsoft HTML Object Library
2.[ComVisible(true)] //放到类的上边
public partial class Form1 : Form
3.//激活webBrowser1_Navigated事件
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
IHTMLWindow2 ihtmlWin = webBrowser1.Document.Window.DomWindow as IHTMLWindow2;
StringBuilder strAlertBuilder = new StringBuilder();
strAlertBuilder.Append("var _alert=window.alert;\n");
strAlertBuilder.Append("window.alert=function(s){\n");
strAlertBuilder.Append("window.external.alertMessage(s);}");
ihtmlWin.execScript(strAlertBuilder.ToString(), "Javascript");
}
4. //接收alert的内容
public void alertMessage(string s)
{
this.textBox1.Text = s;
}
5.关键
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ObjectForScripting = this;//此属性是得到alert关键设置,否则可以禁止弹出,但是无法得到信息
}