如何在Firefox中检测浏览器关闭事件? [英] How to detect browser close event in Firefox?

查看:392
本文介绍了如何在Firefox中检测浏览器关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Firefox浏览器中检测浏览器关闭事件?我想在服务器端进行一些清理,因此我们保持了上次注销时间. 为了实现这一需求,当用户单击注销或关闭浏览器时,触发ajax调用. 对于IE,以下代码有效:

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();

}

如何在Firefox中执行此操作?请帮助我.

How to do this in Firefox? Please help me.

推荐答案

使用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';
};

这篇关于如何在Firefox中检测浏览器关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆