Javascript 处理浏览器关闭和使会话无效
我使用下面的代码检测浏览器关闭事件,然后使会话无效。如果我直接点击浏览器关闭按钮,效果会很好。如果单击页面中的某些,则会触发window.onbeforeunload并使会话无效 如果有人知道这个问题的解决办法,请告诉我Javascript 处理浏览器关闭和使会话无效,javascript,jquery,Javascript,Jquery,我使用下面的代码检测浏览器关闭事件,然后使会话无效。如果我直接点击浏览器关闭按钮,效果会很好。如果单击页面中的某些,则会触发window.onbeforeunload并使会话无效 如果有人知道这个问题的解决办法,请告诉我 /*JS code begins here*/ /*Global js variable to decide whether to call session invalidate function*/ var validNavigation = false;
/*JS code begins here*/
/*Global js variable to decide whether to call session invalidate function*/
var validNavigation = false;
//Called when page loads initially
jQuery(document).ready(function() {
//Call to wireupEvents
wireUpEvents();
});
//Function called when the page loads
function wireUpEvents() {
window.onbeforeunload= function() {
if (!validNavigation) {
/*This JS function calls my managed bean method to invalidate session.*/
windowCloseJsFunction();
}
}
// Attach the event keypress to exclude the F5 refresh
jQuery(document).bind('keypress', function(e) {
if (e.keyCode == 116) {
alert('116');
validNavigation = true;
}
});
// Attach the event click for all links in the page
jQuery("a").bind("click", function() {
alert('click a');
validNavigation = true;
});
// Attach the event submit for all forms in the page
jQuery("form").bind("submit", function() {
alert('form');
validNavigation = true;
});
// Attach the event click for all inputs in the page
jQuery("input[type=submit]").bind("click", function() {
alert('input');
validNavigation = true;
});
//Attach button click for all inputs in the page
jQuery("input[type=button]").bind("click", function() {
validNavigation = true;
});
}
/*JS code ends here*/
为什么首先需要使会话无效?为什么超时解决方案对您不起作用?会话超时设置为30分钟,但在此之前,如果我关闭浏览器,那么我需要等待30分钟重新登录,因为我将当前会话保持为1“那么我需要等待30分钟重新登录为”-为什么?如果这是一个问题-只需保持最新的会议。解决问题的根源,而不是后果。在我通过spring security的应用程序中,最大会话设置为1,因此任何时候每个用户都可以有一个会话。如果我关闭浏览器,那么会话将不会失效,并且我无法重新登录。因此,请以删除旧会话并创建另一个会话的方式实现它。否则,你将毫无实际理由地给用户带来巨大的麻烦。