I\'d like to warn users when they try to close a browser window if they didn\'t save the changes they made in the web form.
I\'m us
Simply add the window.onbeforeunload
event. This shows up a modal window where you can decide to stay or leave the page.
window.onbeforeunload = function (event) {
var message = 'All changes will get lost!';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
}
All browsers will provide a standard message ("Are you sure you want to navigate away from this page?") plus your custom message.
So the window will look like this:
---------------------------------
| Are you sure you want to |
| navigate away from this page? |
| |
| All changes will get lost! |
| |
| [Cancel] [Ok] |
---------------------------------