Q:

Window.unload is not showing alert message on Closing

I want browser tab to show an alert message as soon as it is clicked to close .Here is the full code that i am trying in HTML page..
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <script src="js/jquery-1.7.1.js" type="text/javascript"></script>

     <script type="text/javascript">
        $( window ).unload(function() {
            alert( "Bye now!" ); 
        });
    </script>
</head>
<body>

</body>

But on closing browser tab i am not getting any type of alert message ..Please help me ..

A:

Have you tried/considered onbeforeunload? 
window.onbeforeunload = function(e) {
  return 'Bye now!'; 
};

This event fires when a window is about to unload.  The content is still visible and the user can still cancel and stay on the page. 
Demo

