You have to return
from the onbeforeunload
:
window.onbeforeunload = function() {
saveFormData();
return null;
}
function saveFormData() {
console.log('saved');
}
UPDATE
as per comments, alert does not seem to be working on newer versions anymore, anything else goes 🙂
FROM MDN
Since 25 May 2011, the HTML5 specification states that calls to
window.showModalDialog()
,window.alert()
,window.confirm()
, andwindow.prompt()
methods may be ignored during this event.
It is also suggested to use this through the addEventListener
interface:
You can and should handle this event through
window.addEventListener()
and thebeforeunload
event.
The updated code will now look like this:
window.addEventListener("beforeunload", function (e) {
saveFormData();
(e || window.event).returnValue = null;
return null;
});