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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5