Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
I am creating website in that login is there, after login i stores username & password in session values, after login i close browser directly when i open that page its open without login.
I want to clear session on browser or tab close. I am using below script for that but its not working.

  <script type ="text/javascript" >
         var clicked = false;
         var xmlHttp
         var browser = navigator.appName;

         function CheckBrowser() {

             if (clicked == false) {
                 xmlHttp = GetXmlHttpObject();
       "GET", "Default.aspx", true);
                 xmlHttp.onreadystatechange = function () {
                     if (xmlHttp.readyState == 4) {
                         // alert(xmlhttp.responseText)
                 if (browser == "Netscape") {
                     window.location = ".../Default.aspx";
                     alert("Browser Terminated");
             else {
                 clicked = false;

         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;

         function openInNewWindow() {
             // Change "_blank" to something like "newWindow" to load all links in the same new window
             var newWindow =".../Default.aspx");
             return false;

<body onclick="clicked=true;" onunload="CheckBrowser()">
Updated 5-Jun-13 19:42pm
vaibhav10Dec1987 6-Jun-13 1:28am    
You can use on application_end event
In that you may do "session.abandon()" in global.asax
Sachin Mortale 6-Jun-13 1:35am    
I used it but its not working.

Share this answer
Thanks7872 6-Jun-13 2:28am    
↑voted....Good one...
Have a look at the below link.The link provided by[^] is also usefull.

Ending Session when user close window[^]

It describes each and every aspect in very concise way. You will get complete idea regarding Session.Abandon() and what happens and how to do this on browser close.
Share this answer
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900