Disabling alert window in WebBrowser control

13,116

Solution 1

Try

WebBrowser1.ScriptErrorsSuppressed = true;

Solution 2

Try this

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;

Source : http://moshez.blogspot.co.il/2013/08/c-disable-alert-box-javascript-in-c.html

Solution 3

    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
Share:
13,116
Vytas P.
Author by

Vytas P.

Web Developer from Ukraine. PHP Sample Code Java Programmer Notes Blogger Templates and Help PHP String Compare

Updated on June 04, 2022

Comments

  • Vytas P.
    Vytas P. almost 2 years

    i am using VS 2005 and WebBrowser control. When i using component to navigate to specified page i get scenario error, like "On this page javascript scenario error" and two buttons: yes or no executing scenario. How to prevent alert windows and continue browsing without this messages?

  • Farzin Kanzi
    Farzin Kanzi about 7 years
    Very good, Very good. I don't know why previous answer accepted and high upvoted? it is deserve to acception.
  • Christian
    Christian over 4 years
    To a certain degree what Farzin is stating is correct. Except; OP is not asking to suppress Alert. He is asking to suppress JS error popup. Moshes fix is for the Alert.
  • Christian
    Christian over 4 years
    VB C#... Potato potato. Its all .NET anyway :)