Mozilla的Firefox不能使用window.onbeforeunload [英] Mozilla firefox not working with window.onbeforeunload

查看:311
本文介绍了Mozilla的Firefox不能使用window.onbeforeunload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 window.onbeforeunload 在windows关闭时向用户显示消息,该功能在Chrome和IE中运行良好,但不适用于Firefox ,我使用的是Firefox的版本 26.0 我已经尝试了很多,但没有任何意思,有人说,它在Firefox中的错误,因为在这个信息,另一个建议一些解决方案,如帖子我尝试了所有可用的解决方案,使用JavaScript和jQuery,但它不工作,现在我显示确认对话框,但浏览器默认对话框出现后,我不满意,我也试图阻止浏览器默认对话框出现使用 preventDefault(),但也与没意思!如果有任何解决这个问题,这将是伟大的,这是我如何使用 window.onbeforeunload

I'm using window.onbeforeunload to display a message to the user on windows close, the function works well with chrome and IE but it doesn't work with Firefox, i'm using firefox version 26.0 i have tried many but with no mean, somebody said that its a bug in firefox as in this post and another suggests some solutions as in this post i tried all the solutions available using javascript and jquery but it doesn't work, now i display a confirm dialog but the browser default dialog appears after it and i'm not satisfied with that, i tried also to prevent the browser default dialog from appearing using preventDefault() but also with no mean!! if there's any solution to this problem it will be great, here's how i used the window.onbeforeunload:

<script>
window.onbeforeunload = confirmWinClose();
function confirmWinClose() {
     var myVar ='${isFireFox}';
     if(myVar=='true'){
         return confirm(confirmExamClose);
     }else{
         return confirmExamClose;
     }

}
<script>

注意: isFireFox 是一个jsp变量我曾经知道使用 User-Agent 标题和 confirmExamClose 浏览器的类型是我显示给用户。

Note:isFireFox is a jsp variable that i used to know the type of the browser using User-Agent Header and confirmExamClose is the message that i display to the user.

推荐答案

这是Firefox和Chrome的工作解决方案。我还没有在Safari和Opera测试过。

Here is working solution for Firefox and Chrome. I haven't yet tested in Safari and Opera.

var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable

myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
    var confirmationMessage = 'Are you sure to leave the page?';
    (e || window.event).returnValue = confirmationMessage;
    return confirmationMessage;
});

这篇关于Mozilla的Firefox不能使用window.onbeforeunload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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