Click here to Skip to main content
15,890,579 members
Articles / Web Development / ASP.NET
Tip/Trick

How to end user session when browser closed

Rate me:
Please Sign up or sign in to vote.
4.82/5 (26 votes)
8 Feb 2011CPOL 241.7K   34   34
There is no fool-proof technique to catch the browser close event for 100% of time. The trouble lies in the stateless nature of HTTP. I will explain one of them which is very effective and tested

Introduction

How to capture logoff time when user closes browser?

Or

How to end user session when browser closed?

Or

How to end user session when user redirects to another site?

These are some of the frequently asked questions. Normally this is the requirement of any application. There is no fool-proof technique to catch the browser close event for 100% of the time. The trouble lies in the stateless nature of HTTP. I am explaining one of them which is very effective and tested.

I updated the article and now it support all kind of browser .

Using the Code

1. First create a page LogOut.aspx and in Page_Load event, write this code:

ASP.NET
protected void Page_Load(object sender, EventArgs e)
{  
  Session.Abandon();
} 

2. Then add the following JavaScript code in your page or Master Page:

ASP.NET
<script type="text/javascript">
 
var clicked = false;  
 function CheckBrowser()  
   {      
      if (clicked == false)   
         {      
          //Browser closed   
         }        else  
          {  
          //redirected
             clicked = false; 
           } 
   }  
  function bodyUnload() 
   {      
      if (clicked == false)//browser is closed  
          {   
         var request = GetRequest();  
           request.open  ("POST", "../LogOut.aspx", false);    
       request.send();    
        } 
   } 
 
   function GetRequest()  
     {       
     var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlhttp;
      } 
 
</script>

 

3. Add the following code in the body tag of master page:

ASP.NET
<body onunload="bodyUnload();" Onclick="clicked=true;">

Finally the code in Master page like this:

JavaScript
<script language="javascript" type="text/javascript">
    //<![CDATA[</span />

    var clicked = false;
    function CheckBrowser() {
        if (clicked == false) {
            //Browser closed
        }
        else {
            //redirected 
            clicked = false;
        }
    }

    function bodyUnload() {

        if (clicked == false)//browser is closed
        {
            var request = GetRequest();

            request.open("POST", "../LogOut.aspx", false);
            request.send();
            alert('This is close');
        }
    }
    function GetRequest() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlhttp;
} 
</script>

<body onunload="bodyUnload();" onclick="clicked=true;"><span style="font-size: 9pt;">  </span>
JavaScript
<form id="form1" runat="server">

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
I have worked on number of technologies including C# .Net, VB .Net, ASP.Net, LINQ, WCF, X++, and SharePoint,Oracle,Crystal Reports.

http://dotnetfarrukhabbas.blogspot.com

Comments and Discussions

 
GeneralReason for my vote of 4 it is a fantastic and helpful articl... Pin
NileshKRathod14-Oct-11 0:09
NileshKRathod14-Oct-11 0:09 
GeneralNot a complete solution: 'unload' event fires every time use... Pin
sjelen18-Feb-11 4:35
professionalsjelen18-Feb-11 4:35 
GeneralReason for my vote of 1 Does not do what it says on the tin.... Pin
DerekT-P14-Feb-11 1:53
professionalDerekT-P14-Feb-11 1:53 
GeneralSo, you've a function "CheckBrowser" which, if "clicked" is ... Pin
DerekT-P14-Feb-11 1:52
professionalDerekT-P14-Feb-11 1:52 
GeneralHow this classifies as a "Article" is beyond me.... Totally ... Pin
jfriedman8-Feb-11 12:48
jfriedman8-Feb-11 12:48 
GeneralRe: It's not, it is a tip/trick. Perfectly acceptable (after fix... Pin
Indivara8-Feb-11 14:51
professionalIndivara8-Feb-11 14:51 
GeneralRe: when is the function CheckBrowser()called..? Pin
Avinash Ramchandra Shinde10-Feb-12 22:24
Avinash Ramchandra Shinde10-Feb-12 22:24 
GeneralReason for my vote of 3 poorly written Pin
Sandesh M Patil8-Feb-11 1:17
Sandesh M Patil8-Feb-11 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.