WebBrowser小示例(三)
接着上面的示例说,网页的脚本运行时常会出现一些错误的提示框,如果我们在用winform对网页连续操作时就会出现终止现象,处理这个问题,好在微软为做了这一方面:设置ScriptErrorsSuppressed属性为true,这个问题就解决了。
可是我们还会遇到一些非脚本错误的对话框,如alert(),confirm(),windows.Open()等这个就要求我们做些代码处理了,在(二)中的DocumentCompleted事件中添加如下代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
if (webPage.DocumentText.IndexOf("window.open(") > -1 || webPage.DocumentText.IndexOf("window.close()") > -1)
{
webPage.DocumentText = webPage.DocumentText.Replace("window.open(", "window.external.open(").Replace("window.close()", "window.external.close()");
}
HtmlDocument doc = webPage.Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm() {";
s += "return true;";
s += "}";
s += "function alert() {window.location.href='about:blank';}";
win.execScript(s, "javascript");
{
webPage.DocumentText = webPage.DocumentText.Replace("window.open(", "window.external.open(").Replace("window.close()", "window.external.close()");
}
HtmlDocument doc = webPage.Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm() {";
s += "return true;";
s += "}";
s += "function alert() {window.location.href='about:blank';}";
win.execScript(s, "javascript");
不过我们要对mshtml进行引用。这样对webBroswer的操作就基本完成了,也是刚刚接触,不正确的地方请高手指正。谢谢