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)

转载于:https://www.cnblogs.com/ForFreeDom/archive/2009/09/20/1570451.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值