Click here to Skip to main content
15,881,898 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 240.8K   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

 
QuestionCrisis averted Pin
Member 1449430311-Jun-19 2:25
Member 1449430311-Jun-19 2:25 
QuestionNot Working Logout.aspx Pin
Member 1365131511-Feb-18 20:15
Member 1365131511-Feb-18 20:15 
QuestionHo Do I Use Without Refresh Pin
Member 113230896-Nov-17 2:33
Member 113230896-Nov-17 2:33 
BugSession abandon when refresh and back button Pin
Member 1346945116-Oct-17 22:07
Member 1346945116-Oct-17 22:07 
QuestionChrome and Firefox Pin
Nelson634020-Jun-17 10:42
Nelson634020-Jun-17 10:42 
QuestionQuestion regarding sample code Pin
Kashif Ansari 12312-Sep-15 23:17
Kashif Ansari 12312-Sep-15 23:17 
QuestionPoint 2 Pin
Karan Chhabria26-Aug-15 22:02
Karan Chhabria26-Aug-15 22:02 
AnswerRe: Point 2 Pin
M.Farrukh Abbas28-Aug-15 2:02
M.Farrukh Abbas28-Aug-15 2:02 
BugNot working for page referesh and ispostback Pin
kinal371-Jul-15 8:50
kinal371-Jul-15 8:50 
GeneralRe: Not working for page referesh and ispostback Pin
M.Farrukh Abbas28-Aug-15 2:05
M.Farrukh Abbas28-Aug-15 2:05 
GeneralRe: Not working for page referesh and ispostback Pin
Member 1339602015-Nov-18 1:23
Member 1339602015-Nov-18 1:23 
QuestionQuestion for all browser Pin
mayur_man3-Mar-15 15:38
mayur_man3-Mar-15 15:38 
AnswerRe: Question for all browser Pin
M.Farrukh Abbas6-May-15 23:29
M.Farrukh Abbas6-May-15 23:29 
Question1 Doubt Pin
jakss1-Mar-15 19:02
jakss1-Mar-15 19:02 
QuestionFirefox Pin
Member 109217103-Jul-14 2:16
Member 109217103-Jul-14 2:16 
QuestionHow to end user session when browser closed Pin
basitsar1-Dec-13 0:27
basitsar1-Dec-13 0:27 
AnswerRe: How to end user session when browser closed Pin
Member 269551312-Dec-13 14:18
Member 269551312-Dec-13 14:18 
GeneralMy vote of 5 Pin
shaziafazili18-Dec-12 6:47
shaziafazili18-Dec-12 6:47 
QuestionWoW Pin
gfsrdgehfhfd19-Jul-12 4:09
gfsrdgehfhfd19-Jul-12 4:09 
AnswerRe: WoW - But Pin
gfsrdgehfhfd19-Jul-12 4:56
gfsrdgehfhfd19-Jul-12 4:56 
Questiongood trick but one bug Pin
y_srikanth21-Jun-12 5:12
y_srikanth21-Jun-12 5:12 
GeneralMy vote of 5 Pin
Rahul Rajat Singh22-May-12 18:10
professionalRahul Rajat Singh22-May-12 18:10 
QuestionPerfect code Pin
Member 379524815-May-12 23:58
Member 379524815-May-12 23:58 
QuestionBrowser Close Event Pin
Member 856404529-Mar-12 20:31
Member 856404529-Mar-12 20:31 
GeneralReason for my vote of 1 Wtf Pin
johannesnestler5-Feb-12 21:16
johannesnestler5-Feb-12 21:16 

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.