70

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?

JoJo
  • 19,587
  • 34
  • 106
  • 162
  • possible duplicate of [Crossbrowser onbeforeunload?](http://stackoverflow.com/questions/7389554/crossbrowser-onbeforeunload) – Aelios May 13 '14 at 08:22

4 Answers4

52

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 9
    This can be used for both good and evil. **GOOD**: warning someone that your webcast will be shut down, unsaved changed, ... **BAD**: virus sites that do reverse psychology: "Click cancel to not install the virus". – JoJo Mar 22 '11 at 23:05
  • 2
    This is definitely a feature. The problems associated with leaving a page on which data have been entered are universal. I don't need a more aggressive warning, because 99% of such messages are annoying or misleading. Your app is not special. – Wayne Mar 23 '11 at 02:10
  • 18
    Well for example, my webradio has a flash player and when it's running I'm displaying "if you close this page the stream will stop playing" message. That's pretty different from "you may lose data". – ThiefMaster Mar 23 '11 at 07:08
  • 5
    Same with my web app. There's no data to be lost. The webcast will be stopped. When doing user testing, I noticed a lot of people don't understand the nature of live webcasting. They don't know that when the browser is closed, their webcast stops. They think it works like Youtube where you upload it once and it remains in the cloud forever. – JoJo Mar 23 '11 at 07:26
  • 17
    My application has numerous reasons why it might stop you. Maybe something isn't saved, maybe something is in an error state, maybe an AJAX request hasn't returned yet so I'd like to ask you to wait a second. I should be able to at least tell the user why they are being stopped, otherwise how will they know how to fix it? I think there should be an obvious default message ("data you have entered may not be saved"), and an obvious "custom" message (something that is obviously from the page, and not the browser). Like adverts that say "advertisement" above them so the user won't be mislead. – Slobaum Apr 18 '11 at 20:58
  • See also bug [641509](https://bugzilla.mozilla.org/show_bug.cgi?id=641509), the request to have this "feature" removed. As discussed in 588292, there are ways to have custom messages that also avoid the possibility of attack sites confusing users - there's clearly a usability loss for an otherwise solvable security gain. – Mac Sep 18 '11 at 10:27
32

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. :)

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Nasif
  • 447
  • 1
  • 5
  • 9
  • 4
    @Nasif, First of all excellent code. But Your window.setTimeout(function() { window.stop(); }, 1); does not work when you click the x ie close button of firefox tab. whereas the default popup of "Leave window" does work. – user367134 Sep 06 '12 at 13:39
  • Yes the above code is for page leave, not for the window close Thanks. – Nasif Dec 31 '12 at 08:24
  • OhMyGod! This totally bypasses Mozilla's "security" fix. Thank God because I needed an unload message, but this is totally wrong. Mozilla should definitely prevent this from being possible, but also prevent people trying to find workarounds by *reinstating the unload message*! Stupid stupid decision. – Robin Winslow Feb 15 '13 at 12:25
  • There is no way to inject your own message into the Firefox dialog unfortunately: http://mxr.mozilla.org/mozilla-central/source/layout/base/nsDocumentViewer.cpp#1140. https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/beforeunload is misleading too, the returnValue becomes a boolean and is not actually displayed. – Lekensteyn Mar 08 '13 at 17:22
  • Totally never knew about `RegExp.$1` - +1 – jpillora Jun 03 '13 at 23:41
  • 5
    Does not work in Firefox 23. Appears to have been removed in Firefox 17: [bug 391834](https://bugzilla.mozilla.org/show_bug.cgi?id=391834) – Roman Starkov Sep 05 '13 at 18:48
  • Works in FF 38.0.1 when leaving the site by a link but not on F5/Pagereload and not on cosing window or tab – steven May 22 '15 at 17:20
  • Not working in FF 31 or 38.0.5, neither on page reload nor when navigating away via a link. Both throw an `NS_ERROR_NOT_AVAILABLE` error message and nothing displays. – sp00n Jul 06 '15 at 09:03
3

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.)

xmedeko
  • 7,336
  • 6
  • 55
  • 85
  • this will show two dialogs when closing the tab/browser, not? – ThdK Aug 27 '12 at 13:52
  • @ThdK: Yeah, it shows two dialogues, I have added a note to the post. – xmedeko Aug 28 '12 at 08:26
  • If you remove the line 'return "Blah blah."; ' it only shows the first dialog – gomes May 20 '15 at 12:15
  • @gomes The return statement is crucial. We want to show the second confirm leave or stay dialogue. If you remove it, you have just the alert dialogue. – xmedeko May 20 '15 at 12:24
  • @xmedeko Yes, but if you replace it with a confirm dialogue it is the same as the second dialog, except for the confirmation buttons. See my answer below. – gomes May 20 '15 at 16:40
  • @xmedeko Never mind, I thought if you replaced the alert with a confirm and removed the return statement, it will work. – gomes May 20 '15 at 16:52
1

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.

Jguru
  • 29
  • 1
  • 1
    Even if the user says no to the "Are your sure??" dialog, it doesn't prevent the FF4 message from displaying, or the page from trying to unload. – pkaeding May 15 '11 at 00:41
  • Yes, but this allows you to give the user a message, at least until this Bug will be fixed, I'd suggested an alert, but it seems that then the unload doesn't show any confirm question – Jguru May 16 '11 at 07:24
  • 1
    According to the HTML5 spec calls to prompt,alert, confirm etc. may be ignored. In my case, confirm did not work. – IntelliData Sep 10 '15 at 16:38