简体   繁体   English

如何检测 Firefox 中的浏览器关闭事件?

[英]How to detect browser close event in Firefox?

How to detect browser close event in Firefox browser?如何在 Firefox 浏览器中检测浏览器关闭事件? 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.为了实现这一点,需要在用户单击注销或浏览器关闭时触发 ajax 调用。 For IE the following code is working:对于 IE,以下代码有效:

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?如何在 Firefox 中做到这一点? Please help me.请帮我。

Use onbeforeunload使用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';
};
window.addEventListener('unload',fn,false);
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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM