Click here to Skip to main content
Full site     10M members (30.3K online)    

How to end user session when browser closed

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.

 

Using the Code

 

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

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

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

 
<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  ("GET", "../LogOut.aspx", true);    
       request.send();    
        } 
   } 
 
   function GetRequest()  
     {       
     	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
      } 
 
</script>
 

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

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

Finally the code in Master page like this:

 
<script language="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();

//location.href="/KB/tips/LogOut.aspx";
var request = GetRequest();
 
request.open("GET", "../LogOut.aspx", true);
request.send();
}
}
function GetRequest() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
} 
 
//]]>
</script;"
 
<body onunload="bodyUnload();" onclick="clicked=true;">
<form id="form1" runat="server">
 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
GeneralMy vote of 5
shaziafazili
18 Dec '12 - 6:47 
QuestionWoW
gfsrdgehfhfd
19 Jul '12 - 4:09 
AnswerRe: WoW - But
gfsrdgehfhfd
19 Jul '12 - 4:56 
Questiongood trick but one bug
y_srikanth
21 Jun '12 - 5:12 
GeneralMy vote of 5
Rahul Rajat Singh
22 May '12 - 18:10 
QuestionPerfect code
Member 3795248
15 May '12 - 23:58 
QuestionBrowser Close Event [modified]
Member 8564045
29 Mar '12 - 20:31 
GeneralReason for my vote of 1 Wtf
johannesnestler
5 Feb '12 - 21:16 
GeneralReason for my vote of 4 it is a fantastic and helpful articl...
NileshKRathod
14 Oct '11 - 0:09 
GeneralNot a complete solution: 'unload' event fires every time use...
sjelen
18 Feb '11 - 4:35 
GeneralReason for my vote of 1 Does not do what it says on the tin....
DerekTP123
14 Feb '11 - 1:53 
GeneralSo, you've a function "CheckBrowser" which, if "clicked" is ...
DerekTP123
14 Feb '11 - 1:52 
GeneralHow this classifies as a "Article" is beyond me.... Totally ...
jfriedman
8 Feb '11 - 12:48 
GeneralRe: It's not, it is a tip/trick. Perfectly acceptable (after fix...
Indivara
8 Feb '11 - 14:51 
GeneralRe: when is the function CheckBrowser()called..?
avinashinde2020
10 Feb '12 - 22:24 
GeneralReason for my vote of 3 poorly written
SChristmas
8 Feb '11 - 1:17 

Last Updated 9 Feb 2011 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013