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!