ASP.NET - Kill session when the browser tab closed

Asked By sunil p on 05-Mar-13 06:13 PM

I am working on web application.I want to kill the session when the user closes the browser tab without signout.

I am using


CheckBrowser() {


if (clicked == false) {

xmlHttp = GetXmlHttpObject();

"GET", "SessionClear.aspx", true);

xmlHttp.onreadystatechange =

function () {

if (xmlHttp.readyState == 4) {

// alert(xmlhttp.responseText)





if (browser == "Netscape") {

window.location =



"Browser Terminated");




else {


clicked =





function GetXmlHttpObject() {

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;



body bgcolor="#F2F2F4" style="width: 99%; text-align: center;"


="CheckBrowser()" onclick="clicked=true;"


I have this code in my masterpage.the code is working good.My only problem is it is working for the browser back button also. I just wants to kill the session when the browser tab closed not for the back button.
Please help me anyone.


Robbe Morris replied to sunil p on 05-Mar-13 06:31 PM
I believe onunload is going to fire.  You could look at trying to do something with onbeforeunload.

That said, I can tell you from experience that when the user closes the browser, your code to fire off an xmlhttp request will get aborted before it completes the request.  There is no sure fire way to send a request to the server 100% of the time and expect it to work 100%.

You'll want to have procedures to account for when this fails.  ie support capabilities to unlock a user and that sort of thing.