javascript关闭窗口,清除session

I ' m sorry but I ' ve been extremely busy the last couple of weeks.

You need to create a Logout.aspx page whose sole purpose is to log out the logged 
in  user ( in  the PageLoad event  in  the Logout.aspx page call your Session.Abandon method and  do  whatever  else  you need to  do   in  order to log out the user.). This aspx can be called by the Ajax  in  your window.onbeforeunload.

Here is Ajax that will make a request to the Logout.aspx.
          function  LogOut()  
        {   
/*   Instanciating the HttpRequest object that will be used to make the Ajax calls.   
                See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.
*/  
            
var  xmlHttp;    
            xmlHttp
= GetXmlHttp(); 
            
/*   Logout.aspx is an ASPX page has been created for the sole purpose of logging out the user. */  
            xmlHttp.open(
' GET ' " http://localhost:1259/Logout.aspx " true );  
            
// making the request 
            xmlHttp.send( null );  
        } 
  
        
function  GetXmlHttp() 
        {   
/* This function is responsible for creating an HttpRequest object  
                based on the browser that the user is currently using. 
*/  
            
var  xmlHttp  =   null ;             
            
try  
            {   
// Mozilla, Opera, Safari etc. 
                xmlHttp = XMLHttpRequest(); 
            }
catch (e) 
            {   
// Internet Explorer uses ActiveX objects to make Ajax calls. 
                 // the following are valid versions, here we will loop through  
                 // the versions and attempt to create the ActiveX that matches the browser. 
                 var  versionIds  =  [ " Msxml2.XMLHTTP.6.0 " , " Msxml2.XMLHTTP.5.0 "
                         
" Msxml2.XMLHTTP.4.0 " , " Msxml2.XMLHTTP.3.0 " ,  
                         
" Msxml2.XMLHTTP.2.6 " , " Microsoft.XMLHTTP.1.0 " ,  
                         
" Microsoft.XMLHTTP.1 " , " Microsoft.XMLHTTP " ]; 
                
for ( var  i = 0 ; i < versionIds.length  &&  xmlHttp  ==   null ; i ++ )  
                { 
                    xmlHttp 
=  CreateXmlHttp(versionIds[i]); 
                } 
            } 
           
return  xmlHttp; 
        } 
        
function  CreateXmlHttp(id)  
        {   
/* Creates an ActiveX object used by Internet Explorer that will make Ajax calls */  
            
var  xmlHttp  =   null
            
try   
            { 
                xmlHttp 
=   new  ActiveXObject(id); 
            }
catch (e) {} 
            
return  xmlHttp; 
        } 
  
Now, you need to set your window.onbeforeunload() event to call 
this   function  when the page is about to unload:

      window.onbeforeunload =   function (){LogOut();} 
  
As you have already discovered, the window.onbeforeunload event is called every time the page unloads. In other words, 
this  means that  if  your user causes a page to postback, the page will be sent to the server and unloaded, (then the server does processing and sends back a  new  page which the web browser renders).

This means that you need to have a JavaScript 
function  that will register all valid postback controlsand you need to check  this  before you call your Ajax that logs out the user

    window.onbeforeunload = function () 
     {   
          
if (AValidPostback == false ){LogOut();}   
      } 
  
The AValidPostback is a JavaScript 
boolean  variable that will have to be set to  true   if  the user clicks a control that causes a full page postback

Therefore every single one of your buttons, links etc have to set 
this  value


eg
 myButton.attributes.add( " onclick " , " AValidPostback=true; "
  
Make sure to declare the JavaScript variable 
" AValidPostback "   in  your page somewhere.

I hope 
this  makes things clear

 转自(http://bytes.com/topic/net/answers/797031-logout-user-windows-close#post3181671)

你可能感兴趣的:(JavaScript)