Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检测IE和Fireforx中的退出事件_Javascript_Jquery - Fatal编程技术网

Javascript 检测IE和Fireforx中的退出事件

Javascript 检测IE和Fireforx中的退出事件,javascript,jquery,Javascript,Jquery,我想在所有浏览器上检测退出事件,我尝试了以下代码: <html> <head> <script type="text/javascript"> window.onbeforeunload = function (event) { var message = 'Important: Please click on \'Save\' button to leave this page.'; if (typeof event == 'undefine

我想在所有浏览器上检测退出事件,我尝试了以下代码:

<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function (event) {
    var message = 'Important: Please click on \'Save\' button to leave this page.';
    if (typeof event == 'undefined') {
        event = window.event;
    }
    if (event) {
        event.returnValue = message;
    }
    return message;
};
</script>
</head>
<body>
<p>
Exit your browser .... 
</p>
</body>
</html> 

window.onbeforeunload=函数(事件){
var消息='重要提示:请点击'保存'按钮离开此页面';
如果(事件类型==“未定义”){
event=window.event;
}
如果(事件){
event.returnValue=消息;
}
返回消息;
};

退出您的浏览器。。。。

这段代码在Chrome和safari中有效,但在IE 11和Fireforx 50.0.1中不起作用。。。有什么想法吗

试试看 addEventListener('beforeunload',函数(evt){…

在下面的注释中被提到

另外请注意,各种移动浏览器都会忽略事件的结果(也就是说,它们不会要求用户进行确认)。Firefox在about:config中有一个隐藏的首选项来执行相同的操作。本质上,这意味着用户始终会确认可以卸载文档

about:config
中的隐藏键可以通过键
dom找到。禁用\u beforeunload
dom。需要用户交互\u beforeunload


您的代码在其他方面似乎很好,因此查看您的配置文件可能会有所帮助(注意中提到了移动浏览器,但是我的桌面浏览器上也有这些设置)

这是我发现的响应,它对我有用

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  (e || window.event).returnValue = confirmationMessage; //Gecko + IE
  return confirmationMessage;                            //Webkit, Safari, Chrome
});

谢谢你

它不会被调用?或者在没有任何影响的情况下执行?在IE中它不会被调用!@Mouaici\u Med取决于IE版本(这很重要!!)可能是你需要一种不同的方法来监听可能正在发生的事件thrown@Mouaici_Med是的,在我答案中的链接中,还有一个指向页面的链接;)谢谢你的回复。我正在开发一个软件,该软件将由客户端poste使用,因此你的解决方案需要在所有客户端POST中进行此配置?@Dexion你传递给我的链接中就是这样的