Firefox 4 onBeforeUnload custom message
Asked Answered
T

4

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?

Theodoratheodore answered 22/3, 2011 at 22:44 Comment(1)
possible duplicate of Crossbrowser onbeforeunload?Araiza
A
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.

Aminaamine answered 22/3, 2011 at 22:46 Comment(6)
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".Theodoratheodore
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.Vinylidene
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".Aminaamine
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.Theodoratheodore
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.Sarcenet
See also bug 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.Checked
P
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. :)

Pistoleer answered 26/3, 2012 at 4:8 Comment(8)
@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.Womankind
Yes the above code is for page leave, not for the window close Thanks.Pistoleer
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.Highcolored
There is no way to inject your own message into the Firefox dialog unfortunately: mxr.mozilla.org/mozilla-central/source/layout/base/…. developer.mozilla.org/en-US/docs/Mozilla_event_reference/… is misleading too, the returnValue becomes a boolean and is not actually displayed.Nice
Totally never knew about RegExp.$1 - +1Splice
Does not work in Firefox 23. Appears to have been removed in Firefox 17: bug 391834Livvy
Works in FF 38.0.1 when leaving the site by a link but not on F5/Pagereload and not on cosing window or tabMccracken
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.Swigart
U
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.)

Unending answered 15/3, 2012 at 10:20 Comment(6)
this will show two dialogs when closing the tab/browser, not?Ascocarp
@ThdK: Yeah, it shows two dialogues, I have added a note to the post.Unending
If you remove the line 'return "Blah blah."; ' it only shows the first dialogGlassware
@Glassware 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.Unending
@Unending 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.Glassware
@Unending Never mind, I thought if you replaced the alert with a confirm and removed the return statement, it will work.Glassware
H
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.

Hypoglycemia answered 13/5, 2011 at 15:4 Comment(3)
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.Contributor
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 questionHypoglycemia
According to the HTML5 spec calls to prompt,alert, confirm etc. may be ignored. In my case, confirm did not work.Imprimis

© 2022 - 2024 — McMap. All rights reserved.