简体   繁体   中英

How to block onbeforeload JavaScript in web browser control?

Currently I am using the following code but it is not working:

private void InjectAlertBlocker()
{
    string alertBlocker = @"window.alert = function () { }; 
                            window.print = function () { }; 
                            window.open = function () { }; 
                            window.onunload = function () { }; 
                            window.onbeforeunload = function () { };";

    webBrowser1.Document.InvokeScript("execScript", 
                                      new Object[] { alertBlocker, "JavaScript" });
}

http://www.ehrs.info/h/blea.php

It blocks the alert if you click on 'Test' but not if you navigate away or refresh.

How to completely block this function?

I managed to solve it by injecting the alert blocker on the ProgressChanged event instead of DocumentCompleted.

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        InjectAlertBlocker();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM