Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop alert javascript popup in webbrowser c# 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 show a popup, how can I bypass the popup without using sendkeys enter??

like image 941
robert Avatar asked Oct 03 '10 04:10

robert


2 Answers

If you intend not to ever use the alert() function on your page, you can also just override it. E.g.:

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

If you do need to use JavaScript's alert function, you can 'overload' it:

<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>
like image 55
Andrew Avatar answered Sep 23 '22 15:09

Andrew


In the ProgressChanged event handler, you insert a script element that replaces the Javascript alert function with a function of your own, that 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);
        }
    }

For this to work, you need to add a reference to Microsoft.mshtml and use mshtml; in your form.

like image 21
luvieere Avatar answered Sep 25 '22 15:09

luvieere