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?
In order to set a confirmation message before the user is closing the window you can use
jQuery
Javascript
It's important to notice that you can't put
confirm/alert
insidebeforeunload
Here are the results using the browsers I have access to:
Chrome:
Firefox:
Safari:
IE:
More information regarding the browsers support and the removal of the custom message:
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.
Not anymore. All major browsers have started ignoring the actual message and just showing their own.
Correct. A long time ago, you could use
confirm
oralert
, more recently you could return a string from anonbeforeunload
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 usereturnValue
on the original event:or the old-fasioned way:
That's all you can do.