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

在网页退出时或者关闭浏览器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 代码,稍稍改改就可以了。

除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.