我正在使用VS 2005和WebBrowser控件。当我使用组件导航到指定页面时,我得到方案错误,例如“在此页面上的javascript场景错误”和两个按钮:是或否执行场景。如何防止警报窗口并在没有此消息的情况下继续浏览?
答案 0 :(得分:8)
尝试
WebBrowser1.ScriptErrorsSuppressed = true;
答案 1 :(得分:3)
试试这个
HtmlElement head = WebBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = WebBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string alertBlocker = @"window.alert = function () { }; window.confirm=function () { };";
element.text = alertBlocker;
head.AppendChild(scriptEl);
WebBrowser1.ScriptErrorsSuppressed = true;
资料来源: http://moshez.blogspot.co.il/2013/08/c-disable-alert-box-javascript-in-c.html
答案 2 :(得分:1)
Private Sub WebBrowser1_StatusTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.StatusTextChanged
On Error Resume Next
Dim head As HtmlElement = WebBrowser1.Document.GetElementsByTagName("head")(0)
Dim script As HtmlElement
script = WebBrowser1.Document.CreateElement("script")
script.SetAttribute("type", "text/javascript")
Dim alertBlocker As String = "window.alert = function () { }; window.confirm=function () { }; "
script.SetAttribute("text", alertBlocker)
head.AppendChild(script)
End Sub