Shabat Closer

Thursday, August 1, 2013

C# : Disable Alert box javascript in c# webbrower control

if you want to disable all alerts in a website in your webbrower.

the following code disable :

  • Alert window.
  • Print window
  • confirm window.


/************Disable alert/print function()**********************/
HtmlElement head = WebBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = WebBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string alertBlocker = @"window.alert = function () { }; 
                        window.print=function () { };
                        window.confirm=function () { };
                    ";
element.text = alertBlocker;
head.AppendChild(scriptEl);
WebBrowser1.ScriptErrorsSuppressed = true;
/****************************************************************/


Enjoy!

2 comments:

  1. Prezado, teria o mesmo exemplo em delphi?
    Dear, would the same example in delphi?

    ReplyDelete
  2. Hi,
    How to use VB.NET (Visual basic) this code, i want suppress on youtube "leave this page" alert.
    Thanks.

    ReplyDelete