126

When using window.onbeforeunload (or $(window).on("beforeunload")), 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?

7 Answers 7

235

tl;dr - You can't set custom message anymore in most modern browsers

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. In case you still do need to support old browsers - you can find the information below.

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
16
  • 13
    NOT all browsers support this – Actually which of them do support it? Commented Aug 10, 2016 at 18:49
  • Chrome, Safari, Firefox, IE
    – Dekel
    Commented Aug 10, 2016 at 18:53
  • 14
    I tested on Chrome, and it's not working. It does not display You have some unsaved changes but another message. My question is specifically on if it's possible to display a custom message. I don't care if it displays the default content as well. I just want the text we're passing in to be displayed somewhere. Commented Aug 10, 2016 at 19:05
  • 4
    Exactly. That's what I'm trying to point: is there a fix for the recent versions of Chrome and Firefox? Commented Aug 14, 2016 at 12:17
  • 3
    This answer should be updated to make it clearer that the bulk of it is out of date.
    – tremby
    Commented Jan 9, 2019 at 1:16
40

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.

18

As of 2021, for security reasons, it is no longer possible to display a custom message in the beforeunload popup, at least in the main browsers (Chrome, Firefox, Safari, Edge, Opera).

This is no longer possible since:

  • Chrome: version 51
  • Firefox: version 44
  • Safari: version 9
  • Edge: it has never been possible
  • Opera: version 38

For details see:

An alternative approach in order to get a similar result is to catch click events related to links (that would take you away from the current page) and ask for confirmation there. It might be adjusted to include forms submission or perhaps redirections through scripts (that would require to apply a specific class and information in the elements that trigger the redirect).

Here is a working code snippet (based on jQuery) that shows you how you can do it:

Edit: the code snippet here in SO does not work on all browsers, for security reasons (the snippet generates an iframe... and in some browsers "Use of window.confirm is not allowed in different origin-domain iframes"), but the code DOES work, so give it a try!

$('body').on('click', function(e) {
  var target, href;
  //Identifying target object
  target = $(e.target);

  //If the target object is a link or is contained in a link we show the confirmation message
  if (e.target.tagName === 'A' || target.parents('a').length > 0) {
    //Default behavior is prevented (the link doesn't work)
    e.preventDefault();

    if (window.confirm("Are you really really sure you want to continue?")) {

      //Identify link target
      if (e.target.tagName === 'A') {
        href = target.attr('href');
      } else {
        href = target.parents('a').first().attr('href');
      }

      //Redirect
      window.location.href = href;

    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="https://stackoverflow.com">Click me and I'll take you home</a>

4
  • 1
    this seems it works only when the user clicks on <a>. What if user forced refresh through Ctrl + R? This answer is too limited and cannot be relied upon Commented Aug 28, 2021 at 1:08
  • @KarueBensonKarue I just proposed a different approach. If you need to be covered 360°, you may still use what I proposed in combination with the beforeunload event, having a customized message when links are clicked.
    – clami219
    Commented Aug 31, 2021 at 10:12
  • But how gmail does it, because gmail still does it? Indeed when you begin to write a mail in gmail, adding destination, order, and content, when you close your gmail window, it still prohibit closing the windows with a question! is it a special behavior only allowed on gmail and few other web site? Commented Jun 14, 2023 at 21:40
  • 1
    @NicolasBourdon Gmail uses indeed the beforeunload event, but I don't see a custom message being used there. This is actually what the OP's question is all about.
    – clami219
    Commented Nov 13, 2023 at 12:15
8

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/

6
  • 1
    Andy, would you mind sharing a demo link of this in action? Commented May 1, 2019 at 23:41
  • 1
    Sure. Here is a fiddle with plain/jquery JS: Fiddle Commented May 7, 2019 at 12:27
  • Thank you Andy. Curious to test this across various os/browsers. Commented May 7, 2019 at 18:55
  • U r right.. but actually point is "onbeforeunload" default popup appearing ... can i disable this ?.. i dont want to show this alert .. Commented Mar 2, 2020 at 6:14
  • 4
    Good looking fiddle! Unfortunately (as of May 2020) this only works in Firefox, not Chrome, so I think we may be out of luck going forward.
    – snyderxc
    Commented May 13, 2020 at 20:09
0

You can't set a custom message on a modern browser instead you can use of default alert function.

checkout browser compatibility

-3

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?';
};
0
-4

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.";
}
2
  • 1
    How can you have 2 return statements? Commented Sep 11, 2021 at 10:10
  • @MubarakAwal this is what worked at the time for window.onbeforeunload on the chrome version I had then. now I don't know if anything changed!
    – Juliano
    Commented Sep 20, 2021 at 14:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.