Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox 4 onBeforeUnload custom message

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?

like image 758
JoJo Avatar asked Mar 22 '11 22:03

JoJo


People also ask

Is it possible to display a custom message in the Onbeforeunload popup?

A quick note (since this is an old answer) - these days all major browsers don't support custom message in the beforeunload popup. There is no new way to do this.

What triggers Onbeforeunload?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.


2 Answers

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.

like image 94
ThiefMaster Avatar answered Oct 05 '22 05:10

ThiefMaster


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

like image 21
Nasif Avatar answered Oct 05 '22 04:10

Nasif