浏览器窗口关闭后会话未到期 - Javascript

时间:2016-05-25 11:58:32

标签: javascript html jsp session prototypejs

<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&amp; firefox.It在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框架。我也对其他解决方案持开放态度。

提前致谢。

2 个答案:

答案 0 :(得分:2)

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

如果您需要在用户离开页面时主动终止会话,唯一可行的方法是让您的页面在活动时定期ping服务器,然后让服务器终止会话,如果它没有看到在X分钟内ping。请注意,当选项卡处于打开状态但在后台时,许多浏览器将限制该选项卡的计时器触发的程度,因此您需要尝试使用目标浏览器来为服务器端超时找到适当的间隔。 / p>

答案 1 :(得分:0)

最后,我使用 AJAX / XMLHttpRequest 从以下链接执行此操作:

How to end user session when browser closed