关闭浏览器网页或者tab页时注销session的方法,兼容IE,FIREFOX,CHROME
By:Roy.LiuLast updated:2015-04-13
在网页退出时或者关闭浏览器tab页时,有的系统需要注销用户session,这是需要在关闭时调用系统的注销方法,但刷新操作应该排除在外, 通常是浏览器的 beforeunload 方法,但兼容性不是很好,所以用jquery 包装一下。
引入这段javascript 代码,稍稍改改就可以了。
$(window).on('unload', function(){
});
function getContextPath() {
var pathName = window.location.pathname;
var index = pathName.substr(1).indexOf("/");
var result = pathName.substr(0,index+1);
return result;
}
var validNavigation = true;
function wireUpEvents() {
var dont_confirm_leave = 1; // set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'You sure you want to leave?';
function exitPage(e) {
console.log("exit page");
if (!validNavigation) {
$.get( getContextPath() + "/logout", function(data){});
if (dont_confirm_leave !== 1) {
if (!e){
e = window.event;
}
// e.cancelBubble is supported by IE - this will kill the
// bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
// e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
// return works for Chrome and Safari
return leave_message;
}
}
}
$(window).on('beforeunload', exitPage);
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116) {
validNavigation = true;
}
});
}
$(document).ready(function() {
wireUpEvents();
});
引入这段javascript 代码,稍稍改改就可以了。
From:一号门
Previous:自定义的javascript的验证框架

COMMENTS