Click here to Skip to main content
14,866,384 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Hi
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.

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

         function CheckBrowser() {

             if (clicked == false) {
                 xmlHttp = GetXmlHttpObject();
                 xmlHttp.open("GET", "Default.aspx", true);
                 xmlHttp.onreadystatechange = function () {
                     if (xmlHttp.readyState == 4) {
                         // alert(xmlhttp.responseText)
                     }
                 }
                 xmlHttp.send(null)
                 if (browser == "Netscape") {
                     window.location = ".../Default.aspx";
                     alert("Browser Terminated");
                     openInNewWindow();
                 }
             }
             else {
                 //alert("Redirected"); 
                 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 = window.open(".../Default.aspx");
             newWindow.focus();
             return false;
         }
 
   </script>

<body onclick="clicked=true;" onunload="CheckBrowser()">
Posted
Updated 5-Jun-13 19:42pm
v2
Comments
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.

   
v2
Comments
Thanks7872 6-Jun-13 2:28am
   
↑voted....Good one...
Have a look at the below link.The link provided by amnk.info[^] 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.
   

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