【问题标题】:Session not expiring after browser window close- Javascript浏览器窗口关闭后会话未过期-Javascript
【发布时间】:2016-09-23 00:23:51
【问题描述】:

<script>
		function ServerPing(){       
  var url = "http://localhost:8080/index.html";
  if (window.XMLHttpRequest){ 
    reqXML = new XMLHttpRequest();           
    reqXML.onreadystatechange = HandlePing;   
    reqXML.open("GET", url, true);         
    reqXML.send(null);                        
  }
  else if(window.ActiveXObject){ 
    reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
    if(reqXML){ 
      reqXML.onreadystatechange = HandlePing;   
      reqXML.open("GET", url, true);         
      reqXML.send(null);                     
    }
  }
  else{  
     window.location.href = url;
  }       
}
     
function HandlePing(){
  window.status = "Pinging Server - state: " + reqXML.readyState;
  if(reqXML.readyState == 4){
    var strDate = new Date().toString();
    if(reqXML.status == 200){
      window.status = "Sucessfull Ping at " + strDate;
    }
    else{
      window.status = "Failed Ping at " + strDate;
    }
  }
}
 
window.onload = function(){
  var timer = setInterval("ServerPing()",10000)
}
		
		</script>
<HTML>
<HEAD> 
<TITLE> Test</TITLE>
	
		<script>
		function ServerPing(){       
  var url = "http://localhost:8383/fcpbweb/index.html";
  if (window.XMLHttpRequest){ 
    reqXML = new XMLHttpRequest();           
    reqXML.onreadystatechange = HandlePing;   
    reqXML.open("GET", url, true);         
    reqXML.send(null);                        
  }
  else if(window.ActiveXObject){ 
    reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
    if(reqXML){ 
      reqXML.onreadystatechange = HandlePing;   
      reqXML.open("GET", url, true);         
      reqXML.send(null);                     
    }
  }
  else{  
     window.location.href = url;
  }       
}
     
function HandlePing(){
  window.status = "Pinging Server - state: " + reqXML.readyState;
  if(reqXML.readyState == 4){
    var strDate = new Date().toString();
    if(reqXML.status == 200){
      window.status = "Sucessfull Ping at " + strDate;
    }
    else{
      window.status = "Failed Ping at " + strDate;
    }
  }
}
 
window.onload = function(){
  var timer = setInterval("ServerPing()",10000)
}
		
		</script>
</HEAD>


<BODY>

		<div style="height:300px; width:400px; overflow:hidden; border:1px solid red;">
		
		
		<p style="margin:117px auto 0 auto; width:150px">TEST CONTENT</p>
		
		</div>


</BODY>
</HTML>

当我关闭浏览器选项卡时,我的会话在 IE8、chrome 和 firefox 中没有过期。它在 IE9 中运行良好。我的主要是JSP。

HTML:

<body onbeforeunload="HandleOnClose()"></body>

JS代码:

function HandleOnClose(){

    if (event.clientY < 0) {
                        //event.returnValue = 'Want to leave the page?';
                      location.href=location.protocol+"//"+location.host+"${ctx}/"+logoutLocation;
                    }
                }

我也参考了下面的链接,但没有用。

window.onunload is not working properly in Chrome browser. Can any one help me?

我也在使用Prototypejs 框架。我也对其他解决方案持开放态度。

提前致谢。

【问题讨论】:

    标签: javascript html jsp session prototypejs


    【解决方案1】:

    当用户关闭选项卡时,您无法可靠地通知服务器。根本没有API。您的示例代码存在几个问题,尤其是试图强制用户到 onbeforeunload 处理程序中的新位置是低俗网站使用的众所周知的策略,因此优秀的浏览器会忽略它。事实上,即使是刷新操作(而不是关闭选项卡)也会触发您的处理程序。

    如果您需要在用户离开您的页面时主动终止会话,唯一可靠的方法是让您的页面在活动时定期 ping 服务器,然后让服务器在没有看到会话时终止会话在 X 分钟内 ping。 Beware that when a tab is open but in the background, many browsers will limit the extent to which that tab's timers fire, and so you'll need to experiment with your target browsers to find an appropriate interval for your server-side timeout.

    【讨论】:

    • Crowder:如何 ping 服务器。我没明白。你知道任何例子或博客吗?我只在 JS 中寻找答案。Thnx
    • 我对所有可行的解决方案/选项持开放态度。
    • @MikePhils:您使用“ajax” (XMLHttpRequest) ping 服务器。
    • 我问你是否知道任何正在运行的示例或一些代码参考,如果 Windows/Tab 使用服务器 ping 方法关闭,则从会话中注销。
    • 我用服务器ping方式更新了有问题的代码,不知道浏览器关闭时如何实现。
    【解决方案2】:

    最后,我使用 AJAX/XMLHttpRequest 从下面的链接做同样的事情:

    How to end user session when browser closed

    【讨论】:

      猜你喜欢
      • 2016-12-13
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2011-04-21
      • 1970-01-01
      • 2013-11-17
      相关资源
      最近更新 更多