Firefox 4 onBeforeUnload custom message

JavascriptFirefoxFirefox4

Javascript Problem Overview


In Firefox 3, I was able to write a custom confirmation popup with:

window.onbeforeunload = function() {
   if (someCondition) {
      return 'Your stream will be turned off';
   }
}

Now in Firefox 4, it does not show my custom message. The default message that it provides is not even accurate to what my application does.

firefox 4 confirm

Can this default message be overridden?

Javascript Solutions


Solution 1 - Javascript

From MDN: > Note that in Firefox 4 and later the returned string is not displayed to the user. See Bug 588292.

This "Bug" is actually a (imho questionable) feature.. so there's no way to display the message in Firefox 4. If you think it should be changed, comment on that bug so the Firefox developers will know that people actually want to be able to show a custom string.

Solution 2 - Javascript

Addition to the above Answer, I have improved the workaround.

I have used jquery here. you can use default javascript funciton as well.

$(window).bind('beforeunload', function() {
    if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
        if(confirm("Are you Sure do you want to leave?")) {
            history.go();
        } else {
            window.setTimeout(function() {
                window.stop();
            }, 1);
        }
    } else {
        return "Are you Sure do you want to leave?";
    }
});

Tested and working in firefox 11 as well. :)

Solution 3 - Javascript

My workaround is to show alert in onbeforeunload:

window.onbeforeunload=function() {
    if ( /Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
        alert("Blah blah. You have to confirm you are leaving this page in the next dialogue.");
    }
    return "Blah blah."; 
} 

(It shows two dialogues in Firefox, one dialogue elsewhere.)

Solution 4 - Javascript

Try implementing it with a confirm message,

window.onbeforeunload=function(){
   return confirm("Are you sure??");
}

of course when the user confirms then the FF4 message is shown, so you maybe better display this once per site on login/visit. A cookie should do the trick.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoJoView Question on Stackoverflow
Solution 1 - JavascriptThiefMasterView Answer on Stackoverflow
Solution 2 - JavascriptNasifView Answer on Stackoverflow
Solution 3 - JavascriptxmedekoView Answer on Stackoverflow
Solution 4 - JavascriptJguruView Answer on Stackoverflow