Stop javascript popup in c # webbrowser control

This website: http://blog.joins.com/media/folderListSlide.asp?uid=ddatk&folder=3&list_id=9960150

has this code:

<script>alert('¿Ã¹Ù¸¥ Çü½ÄÀÌ ¾Æ´Õ´Ï´Ù.');</script>

So, my web browser control shows a popup, how can I get around the popup without using sendkeys enter ??

+5
source share
6 answers

In the event handler, ProgressChangedyou insert a script element that replaces the Javascript alertfunction with its function, which does nothing:

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
        {
            HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
            HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
            IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
            string alertBlocker = "window.alert = function () { }";
            element.text = alertBlocker;
            head.AppendChild(scriptEl);
        }
    }

To do this, you need to add a link to Microsoft.mshtmland use mshtml;in your form.

+5
source

alert() , . :.

<script type="text/javascript">
alert = function(){}
</script>

JavaScript, "" :

<script type="text/javascript">
var fnAlert = alert;
alert = function(message,doshow) {
    if (doshow === true) {
        fnAlert(message);
    }
}
alert("You won't see this");
alert("You will see this",true);
</script>
+4

IDocHostShowUI:: ShowMessage S_OK. http://www.codeproject.com/KB/miscctrl/csEXWB.aspx .

+4

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
    if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
    {
        HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
        HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
        IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
        string alertBlocker = "window.alert = function () { }";
        element.text = alertBlocker;
        head.AppendChild(scriptEl);
    }
}

,

+2

, alert(xxx) javascript, WebBroswer WinForm? :

broswer.Navigated += (sender, args) =>
  {
     var document = (sender as WebBrowser).DocumentText;
     //find the alert scripts and remove/replace them
  }
+1
source

You can disable all pop-ups by setting

webBrowser.ScriptErrorsSuppressed = true;

Despite the name, these settings actually block all pop-ups, including alert()

-1
source

All Articles