Click here to Skip to main content
11,436,179 members (63,575 online)
Click here to Skip to main content

How to end user session when browser closed

, 8 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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">

License

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

Share

About the Author

M.Farrukh Abbas
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
Follow on   Google+   LinkedIn

Comments and Discussions

 
QuestionQuestion for all browser Pin
mayur_man3-Mar-15 16:38
membermayur_man3-Mar-15 16:38 
Question1 Doubt [modified] Pin
jakss1-Mar-15 20:02
memberjakss1-Mar-15 20:02 
QuestionFirefox Pin
Member 109217103-Jul-14 3:16
memberMember 109217103-Jul-14 3:16 
QuestionHow to end user session when browser closed Pin
basitsar1-Dec-13 1:27
memberbasitsar1-Dec-13 1:27 
AnswerRe: How to end user session when browser closed Pin
Member 269551312-Dec-13 15:18
memberMember 269551312-Dec-13 15:18 
GeneralMy vote of 5 Pin
shaziafazili18-Dec-12 7:47
membershaziafazili18-Dec-12 7:47 
QuestionWoW Pin
gfsrdgehfhfd19-Jul-12 5:09
membergfsrdgehfhfd19-Jul-12 5:09 
AnswerRe: WoW - But Pin
gfsrdgehfhfd19-Jul-12 5:56
membergfsrdgehfhfd19-Jul-12 5:56 
Questiongood trick but one bug Pin
y_srikanth21-Jun-12 6:12
membery_srikanth21-Jun-12 6:12 
GeneralMy vote of 5 Pin
Rahul Rajat Singh22-May-12 19:10
memberRahul Rajat Singh22-May-12 19:10 
QuestionPerfect code Pin
Member 379524816-May-12 0:58
memberMember 379524816-May-12 0:58 
QuestionBrowser Close Event [modified] Pin
Member 856404529-Mar-12 21:31
memberMember 856404529-Mar-12 21:31 
GeneralReason for my vote of 1 Wtf Pin
johannesnestler5-Feb-12 22:16
memberjohannesnestler5-Feb-12 22:16 
GeneralReason for my vote of 4 it is a fantastic and helpful articl... Pin
NileshKRathod14-Oct-11 1:09
memberNileshKRathod14-Oct-11 1:09 
GeneralNot a complete solution: 'unload' event fires every time use... Pin
sjelen18-Feb-11 5:35
membersjelen18-Feb-11 5:35 
GeneralReason for my vote of 1 Does not do what it says on the tin.... Pin
DerekTP12314-Feb-11 2:53
memberDerekTP12314-Feb-11 2:53 
GeneralSo, you've a function "CheckBrowser" which, if "clicked" is ... Pin
DerekTP12314-Feb-11 2:52
memberDerekTP12314-Feb-11 2:52 
GeneralHow this classifies as a "Article" is beyond me.... Totally ... Pin
jfriedman8-Feb-11 13:48
memberjfriedman8-Feb-11 13:48 
GeneralRe: It's not, it is a tip/trick. Perfectly acceptable (after fix... Pin
Indivara8-Feb-11 15:51
subeditorIndivara8-Feb-11 15:51 
GeneralRe: when is the function CheckBrowser()called..? Pin
avinashinde202010-Feb-12 23:24
memberavinashinde202010-Feb-12 23:24 
GeneralReason for my vote of 3 poorly written Pin
SChristmas8-Feb-11 2:17
memberSChristmas8-Feb-11 2:17 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 9 Feb 2011
Article Copyright 2011 by M.Farrukh Abbas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid