JavaScript: window.onbeforeunload Event - Show “Are you sure you want to leave this page?” Message
Handle window.onbeforeunload event using jQuery
$(window).bind('beforeunload', function() {
return 'You have unsaved changes. If you leave the page these changes will be lost.';
});
Handle window.onbeforeunload event using straight JavaScript
window.onbeforeunload = function(e) {
return 'You have unsaved changes. If you leave the page these changes will be lost.';
};