0

i have this js that requests the user to be logged out whenever they close the tab/window and cancels that request when the user does an window.onload shortly after a few second to distinguish between refresh and exiting the tab/window. it works perfectly when on Chrome but when it comes to mozilla the window.onunload event gets triggered but does not trigger the ajax call. below are my codes

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
            }
        }
    }
};

and here are the cancelSessionTimeoutRequest and requestSessionTimeout definitions

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 Answers1

0

Hey your code working on Google Chrome but not working on Mozilla....

Solution 1:

Means there may be a cache problem first you clear cache then relaoad your webpage

Or

Solution 2:

        event.preventDefault();

Write this code inside windowonload function

Sibin Rasiya
  • 1,132
  • 1
  • 11
  • 15