Take a look at the Jquery.beforeunload property. Here is an example:
$(window).bind('beforeunload', function(){ return 'Click OK to exit'; });
Please note: beforeunload canot does not allow you to unload a page or redirect it to another page for obvious reasons; it would be too easy to abuse. Also, if you just want to run the function before unloading, try the following:
$(window).unload(function(){ alert('Bye.'); });
Finally, remember to pass jQuery to your header tag using:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
The above version contains the latest version from the Internet and eliminates the need to download it, and of course you can do it as you wish, but I'm just trying to get your work to work as soon as possible. Oh, I also found an example for you. Click here to see the page that calls the function before closing it. Hope this helps the kidneys.
Lukas source share