Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Context:

Lots of pages have alerts when you leave them: if you close tabs, navigate away without saving, etc, there are numerous reasons why a site would alert/block you from leaving until you confirm an alert, e.g. "Are you sure you want to navigate away from this page?".

This is typically done with the onbeforeunload and/or onunload handlers.

Here is an example.

Question:

Is there any way that I can prevent alerts/user-blocking events generated by those handlers? Basically, I'd like to leave JS enabled, and specifically disallow things that would prevent me from leaving a page without an extra click from happening.

To clarify: onbeforeunload and onunload handlers should still fire; they just shouldn't be allowed to do things that block the user. That means no alerts, and no operations that take more than a few seconds.

What I've Tried:

I've found a few plugins that edit/greasemonkey patch the javascript for particular pages, and played with their code a bit to try and make them more universally applicable. However, I'm hoping to find a solution that works on any page that tries to block user exit.

share|improve this question
    
Can you post a URL that does that? –  golimar May 8 '13 at 16:11
    
Edited, added example. Also, I understand why this behavior is often desirable, and that it is needed to prevent accidental data loss in many cases. I still would like to know how to override/disable it. –  Zac B May 8 '13 at 17:54
    
Note: all of the current answer (which modify onbeforeunload) will only work for pages that use onbeforeunload, not for pages that use one of the other methods for attaching events. –  Synetech Jan 16 at 4:09

2 Answers 2

Well, the simplest thing to do is simply override the pages script directly, and reassign the event to null.

window.onbeforeunload = null;

This should work anywhere, unless they are truly malicious, and keep reassigning it themselves. In such a case, a loop to keep setting null will probably work.

while(true) {
    if (window.onbeforeunload != null) {
        window.onbeforeunload = null;
    }
}

Now, be warned, some pages use this for good. Take the YouTube upload page: If you navigate away during an upload, you may have lost hours of progress! Or perhaps a web form you are filling out, which you may not want to fill out again, or a message (forum/email) that you may not want to retype. With this feature, you are protected.

Another issue is any page using this for some secondary purpose, like saving data by AJAX. They may use this event to trigger the save action, and hope that the user takes long enough to click the button for the request to go through. Again, this is often done to save you from yourself.

But, obviously, we all know many pages use this for ill. So if you know of any pages you want it working on, you could always have a white list system. There really isn't a way to block this action, without totally blocking window.onbeforeunload, and any (possibly good) actions it may take.

There is no way (without prior knowledge of a given pages code) to keep the good actions while stopping the popup box. This box is not an alert(). The box is generated by the browser, as the intended behavior of onbeforeunload. One creates it by making whatever function they assign to window.onbeforeunload return a string. That string will be printed in the popup.

window.onbeforeunload = function() {
    //Whatever
    return "WARNING! You have unsaved changes that may be lost!";
}

Thus, you would be unable to block the popup without erasing the function.

Plus, if you did find a way, any AJAX would fail. The popup gives requests time to go through, without it, data may be lost.

As for onunload, it should not be possible to block you with this. Since it fires after the page unloads. But to be safe, you can always do a window.onunload = null; and it should be taken care of.

share|improve this answer
    
That sort of works, but what if the onbeforeunload handler did something else important? For example, what it if made a brief AJAX request, or committed something to local HTML5 storage? Is there a way to just prevent onbeforeunload handlers from making certain calls (i.e. alert()), or running for longer than a certain period of time? That's why I asked about an extension rather than just a greasemonkey-type script: it seems like, to do this right, there would have to be something changed at the JavaScript engine level. –  Zac B May 11 '13 at 21:37
1  
I have added some more information. As far as actually fixing this at a lower level, extensions are not actually a lower level. They do have higher permissions, and some special API's, but they lack any ability to change the core functions of the browser. I am not sure how deep FF plugging can go, I have no experience in that area. Obviously, being open source, you could make those changes. But that's going a bit far to fix a popup. –  zeel May 12 '13 at 3:53
    // ==/UserScript==

var th = document.getElementsByTagName('body')[0];
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.innerHTML = "window.onbeforeunload = function() {}";
th.appendChild(s);
share|improve this answer
1  
Why would you ever create a script like this? –  Brad Jan 16 at 4:50
2  
@anywho. It may be helpful if you try to explain your script to help others know what they are doing. This would also allow you answer to be used by many since a specific piece of the script may be able to be altered in a way to improve the script or a piece might be able to be used in a completely different script. It is also a good idea to explain you answer anyways to help people know how it applies to their question. Thank you for posting your answer though. –  David Jan 16 at 5:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.