关闭浏览器网页或者tab页时注销session的方法,兼容IE,FIREFOX,CHROME

摘要: 在网页退出时或者关闭浏览器tab页时,有的系统需要注销用户session,这是需要在关闭时调用系统的注销方法,但刷新操作应该排除在外, 通常是浏览器的 beforeunload 方法,但兼容性不是很好,所以用jquery  包装一下。

在网页退出时或者关闭浏览器tab页时,有的系统需要注销用户session,这是需要在关闭时调用系统的注销方法,但刷新操作应该排除在外, 通常是浏览器的 beforeunload 方法,但兼容性不是很好,所以用jquery 包装一下。

$(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 代码,稍稍改改就可以了。

上一篇: 自定义的javascript的验证框架
下一篇: google authenticator 一次性验证码TOTP java 代码实现
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号