我有一个要求,即在浏览器关闭或标签关闭时注销用户。我尝试使用以下代码,它可以正常工作,但存在以下问题: 1.) 当我单击浏览器刷新按钮时,它仍然会注销 2.) 当我更改浏览器中的URL并按回车键时,它仍然会注销 以上两个问题都是不希望出现的,我想要防止这种情况。有人能建议如何解决吗? 以下是我的脚本:
var validNavigation = false;
function wireUpEvents() {
window.onbeforeunload = function(e) {
if (!validNavigation) {
endSession();
}
}
//Hits the server and invalidates the session
function endSession() {
$.ajax({
url : "performLogout..",
type : "GET",
async: false,
success : function(response) {},
error: function(response) {}
});
}
//On click of F5
document.onkeydown = function disableKeys() {
if( typeof event != 'undefined' ) {
if(event.keyCode == 116) {
validNavigation = true;
event.keyCode = 0;
//return false; //return false if u want to disable refresh
}
}
};
// Attach the event click for all links in the page
$("a").on("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").on("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").on("click", function() {
validNavigation = true;
});
$("select#select-id").on("change", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
谢谢