Javascript无法在Mozilla上运行

时间:2018-02-06 11:42:42

标签: javascript jquery ajax

我有这个js请求用户在他们关闭选项卡/窗口时注销,并在用户在几秒钟之后立即执行window.onload时取消该请求以区分刷新和退出选项卡/窗口。它在Chrome上完美运行但是当涉及到mozilla时,window.onunload事件会被触发,但不会触发ajax调用。以下是我的代码

window.onunload = function(){

setCookie('tabs', parseInt(getCookie('tabs')) - 1, 99999)
if(parseInt(getCookie('tabs')) < 1)
{
if (window.localStorage) {
        window.localStorage['myUnloadEventFlag']=new Date().getTime();
    }
    requestSessionTimeout();

}
};



window.onload = function(){
    setCookie('tabs', parseInt(getCookie('tabs')) + 1, 99999)   
    if(parseInt(getCookie('tabs')) < 2 ){
        if (window.localStorage) {
            var t0 = Number(window.localStorage['myUnloadEventFlag']);
            if (isNaN(t0)){
                t0=0;
            } 
            var t1=new Date().getTime();
            var duration=t1-t0;
            if (duration<10*1000) {
                cancelSessionTimeoutRequest(); // asynchronous AJAX call
            }
        }
    }
};

这里是cancelSessionTimeoutRequest和requestSessionTimeout定义

function requestSessionTimeout(){
    var header = ''
    var token = ''

$.ajax({
        url:antiCsrfHost +"/addSessionToDeleteQueue",
        type:"POST",
        aysnc:false,
        contentType: 'application/json; charset=utf-8',
            success:function(d){
        }
    })
}

function cancelSessionTimeoutRequest(){
$.ajax({
        type: 'GET',
        url: antiCsrfHost + '/csrf' ,
        xhrFields: true,
        contentType: 'application/json; charset=utf-8',
        aysnc:true,
        success: function (data) {
            $.ajax({
                    url:antiCsrfHost +"/removeSessionToDeleteQueue",
                    type:"POST",
                    data : null,
                    dataType : 'json',
                    aysnc:true,
                    contentType: 'application/json; charset=utf-8',
                    beforeSend : function(xhr){
                          xhr.setRequestHeader(data.headerName, data.token);
                         },
                    success:function(d){

                    }
                })
            }
        })
}

1 个答案:

答案 0 :(得分:0)

嘿,您的代码在Google Chrome上工作,但没有在Mozilla上工作....

解决方案1:

意味着可能存在缓存问题,首先清除缓存然后重新加载您的网页

或者

解决方案2:

        event.preventDefault();

将此代码写入windowonload函数