The JavaScript window.onbeforeload() event is fired just before the web page is unloaded. This gives us the ability to make sure that the user is certain about leaving the page that they are viewing. We can also use this to catch the browser closing event.
I have mixed the script with Jquery. We can use Jquery’s bind method to bind the event but using bind we can’t handle the event properly. Using the following script we can easly customize the confirmation message and also it will work on IE and most of the common web browsers.
Actually jquery is not having any functional role in the script but using the following script we can avoid inline scripting and our code will look nice.
$(window).load(function() { window.onbeforeunload = navigateAway; }); function navigateAway(e){ var message = "Your confirmation message goes here.", e = e || window.event; // For IE and Firefox if (e) { e.returnValue = message; } // For Safari return message; }
December 13th, 2009 