P粉1869047312023-08-25 00:34:09
Not anymore. All major browsers started ignoring the actual message and only showing their own.
correct. A long time ago you could use confirm
or alert
, more recently you could return a string handler from onbeforeunload
and the string would be shown. The contents of the string are now ignored and treated as flags.
When using jQuery's on
you do 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 fashioned way:
window.onbeforeunload = function() { return "Your message here"; // Probably won't be shown, see note above };
That's all you can do.
P粉1436404962023-08-25 00:32:48
tl;dr - You can no longer set a custom message in most modern browsers
In order to set a confirmation message before the user closes 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 is important to note that you cannot place confirmation/alert
internal beforeunload< /代码>
Here are the results using a browser I have access to:
Chrome:
Firefox browser:
Safari:
IE:
For more information on browser support and removal of custom messages: