How to detect browser close event in Firefox?

天大地大妈咪最大 提交于 2019-12-14 02:19:02

问题


How to detect browser close event in Firefox browser? I want to do some clean up process in server side and we maintaining last logout time. To achieve this need fire an ajax call when the user clicks logout or the browser is closed. For IE the following code is working:

if (window.event.clientY < 0 && (window.event.clientX > (document.documentElement.clientWidth - 5) || window.event.clientX < 0)) {

        logoutUser();//logging out the user

        } 
}


function logoutUser(){

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
    //Safari
    xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
xmlhttp.open("GET", "Logout.action", true);
xmlhttp.send();

}

How to do this in Firefox? Please help me.


回答1:


Use onbeforeunload

window.onbeforeunload = function (e) {

  e = e || window.event;

  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Any string';
  }

  // For Safari
  return 'Any string';
};



回答2:


var closedWindow = function() {
    //code goes here
};

if (window.addEventListener) {
    window.addEventListener('unload', closedWindow, false);
} else if (window.attachEvent) {
    // pre IE9 browser
    window.attachEvent('onunload', closedWindow);
}



回答3:


window.addEventListener('unload',fn,false);


来源:https://stackoverflow.com/questions/6757485/how-to-detect-browser-close-event-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!