javascript - 浏览器窗口关闭后 session 不会过期 - Javascript

标签 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>

当我关闭浏览器选项卡时,我的 session 在 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框架。我也愿意接受其他解决方案。

提前致谢。

最佳答案

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

如果您需要在用户离开您的页面时主动终止 session ,唯一可靠的方法是让您的页面在事件时例行 ping 服务器,然后让服务器在没有看到 session 时终止 session 。 X 分钟内 ping 通。请注意,当选项卡在后台打开时,许多浏览器将限制该选项卡计时器触发的程度,因此您需要对目标浏览器进行试验,以找到服务器端超时的适当间隔。

关于javascript - 浏览器窗口关闭后 session 不会过期 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37436436/

相关文章:

javascript - 图像无法正确调整到 div 的高度

javascript - GPT "viewability"与 slotRenderEnded 事件触发的确切时刻

javascript - 如何从 AJAX 请求中解析 HTML?

javascript - 如何根据条件使表单字段具有不同的验证模式?

javascript - 强制浏览器下载一个大的 HTML 文件到磁盘并打开它

html - 正文不会拉伸(stretch)到全高页面

html - 将鼠标悬停在另一个元素上时更改元素的属性

java - 需要 txt 文件中的数据(以逗号分隔),以使用现有类中的对象填充数组列表

java - JSP搜索数据库

html - <c :out escapeXml ="false"> and scriptlet 的 HTML 输出差异