Browser close event detection

Browser close event detection

   Many times it requires to notify the server about closing of client side browser.
This way when end user closes his browser you can clear his / her session objects which are not expired yet. Also you can perform other stuff like
make user offline etc.

<html>  
<head>  
<title>Detecting browser close in IE</title>  
<script type="text/javascript">  
var message="If you have made any changes to the fields without clicking the Save button, your changes will be lost.";  
function ConfirmClose(e)  
{  
    var evtobj=window.event? event : e;  
    if(evtobj == e)  
      {  
        //firefox  
          if (!evtobj.clientY)  
          {  
                //evtobj.returnValue = message;  
            
             var img = new Image();

             img.src="ClearSession.aspx";



          }  
      }  
      else 
      {  
      //IE  
        if (evtobj.clientY < 0)  
          {  
   //             evtobj.returnValue = message;   

  
             var img = new Image();

             img.src="ClearSession.aspx";
   
          }  
      }  
}  
</script>  
</head>  
 
 
<body onbeforeunload="ConfirmClose(event)">  
<h4>Close browser!</h4>  
</body>  
</html>  

 
  In above examle I'm calling ClearSession.aspx using Image src trick.

Tags:

How to set default browserWhat is difference between browser reload and refreshJquery textbox KeyUp eventJquery button click eventJquery Get Target Of EventAndroid browser remove safe searchlistbox select eventJquery image click eventNo Visual Studio template information found See the application log in Event Viewer for more detailsMost used browsers 2011session objects

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.