window.onbeforeunload and window.onunload is not working in Firefox, Safari, Opera?
I got the solution for onunload
in all browsers except Opera by changing the Ajax asynchronous request into synchronous request.
xmlhttp.open("POST","LogoutAction",false);
It works well for all browsers except Opera.
Is it possible to display a custom message in the beforeunload popup?
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:
- 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:
Firefox:
Safari:
IE:
Just to make sure - you need to have jquery included
More information regarding the browsers support and the removal of the custom message:
- Chrome removed support for custom message in ver 51
- Opera removed support for custom message in ver 38
- Firefox removed support for custom message in ver 44.0 (still looking for source for this information)
- Safari removed support for custom message in ver 9.1
Related Topics
How to Getelementbyclass Instead of Getelementbyid with JavaScript
How to Sort Array Inside Collection Record in Mongodb
Event Handlers Inside a JavaScript Loop - Need a Closure
What Do Square Brackets Around a Property Name in an Object Literal Mean
Is There a Better Way to Do Optional Function Parameters in JavaScript
When to Use Vanilla JavaScript VS. Jquery
How to Replace Innerhtml of a Div Using Jquery
Most Efficient Way to Convert an HTMLcollection to an Array
How to Use Async/Await at the Top Level
When to Use Setattribute VS .Attribute= in JavaScript
How to Call a JavaScript Function on Page Load
Invariant Violation: Objects Are Not Valid as a React Child
Why Shouldn't Jsx Props Use Arrow Functions or Bind
How to Decode a String with Escaped Unicode
Sort Array by Firstname (Alphabetically) in JavaScript
How to Access Iframe Elements with JavaScript
Navigator.Geolocation.Getcurrentposition Sometimes Works Sometimes Doesn'T