The onbeforeunload event captures each unload event, but there is some trick to handle if the user closes the browser by clicking the close button, here is an example code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ </script> </head> <body> <p> <a href="http://cssbased.com">go outside</a> <br> <a href="test.html">reload page</a> <span> </span> </p> </body> </html>
source share