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

When using window.onbeforeunload (or $(window).on("beforeonload")), is it possible to display a custom message in that popup?

Maybe a small trick that works on major browsers?

By looking at existing answers I have the feeling this was possible in the past using things like confirm or alert or event.returnValue, but now it seems they are not working anymore.

So, how to display a custom message in the beforeunload popup? Is that even/still possible?

Answers:

Answer

In order to set a confirmation message before the user is closing the window you can use

jQuery

$(window).bind("beforeunload",function(event) {
    return "You have some unsaved changes";
});

Javascript

window.onbeforeunload = function() {
    return "Leaving this page will reset the wizard";
};


      It's important to notice that you can't put confirm/alert inside beforeunload


A few more notes:

  1. NOT all browsers support this (more info in the Browser compatibility section on MDN)
  2. In Firefox you MUST do some real interaction with the page in order for this message to appear to the user.
  3. Each browser can add his own text to your message.

Here are the results using the browsers I have access to:

Chrome:

Chrome alert on exit

Firefox:

Firefox alert on exit

Safari:

Safar alert on exit

IE:

IE alert on exit

Just to make sure - you need to have jquery included

More information regarding the browsers support and the removal of the custom message:

  1. Chrome removed support for custom message in ver 51
  2. Opera removed support for custom message in ver 38
  3. Firefox removed support for custom message in ver 44.0 (still looking for source for this information)
  4. Safari removed support for custom message in ver 9.1
Answer

When using window.onbeforeunload (or $(window).on("beforeonload")), is it possible to display a custom message in that popup?

Not anymore. All major browsers have started ignoring the actual message and just showing their own.

By looking at existing answers I have the feeling this was possible in the past using things like confirm or alert or event.returnValue, but now it seems they are not working anymore.

Correct. A long time ago, you could use confirm or alert, more recently you could return a string from an onbeforeunload handler and that string would be displayed. Now, the content of the string is ignored and it's treated as a flag.

When using jQuery's on, you do indeed have to use returnValue on the original event:

$(window).on("beforeunload", function(e) {
    // Your message won't get displayed by modern browsers; the browser's built-in
    // one will be instead. But for older browsers, best to include an actual
    // message instead of just "x" or similar.
    return e.originalEvent.returnValue = "Your message here";
});

or the old-fasioned way:

window.onbeforeunload = function() {
    return "Your message here"; // Probably won't be shown, see note above
};

That's all you can do.

Answer

I just made a div appear that shows a message in the background. It is behind the modal but this is better then nothing. It is kind of shady but at least you can give your user some info on why you bother her/him not to leave.

constructor($elem)
{
    $(window).unbind().bind('beforeunload', (e) => this.beforeUnload(e));
}
beforeUnload(e)
{
    $('#leaveWarning').show();
    setTimeout(function(){
        $('#leaveWarning').hide();
    }, 1); // set this to 1ms .. since timers are stopped for this modal,
           // the message will disappear right after the user clicked one of the options  
    return "This message is not relevant in most modern browsers.";
}

Here is a working Fiddle https://jsfiddle.net/sy3fda05/2/

Answer

All the above doesn't work in chrome at least it need to add return false otherwise nothing happen.

window.onbeforeunload = function(e) {
  $('#leaveWarning').show();

  // the timer is only to let the message box disappear after the user
  // decides to stay on this page
  // set this to 1ms .. since timers are stopped for this modal  
  setTimeout(function() {
    $('#leaveWarning').hide();
  }, 1);

  // 
  return false;
  return "This message is not relevant in most modern browsers.";
}
Answer

Try this code for all all browsers supported

window.onbeforeunload = function (e) {
    e = e || window.event;

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = 'Sure?';
    }

    // For Safari
    return 'Sure?';
};

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.