Mozilla firefox 不适用于 window.onbeforeunload [英] Mozilla firefox not working with window.onbeforeunload

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

问题描述

我正在使用 window.onbeforeunload 在窗口关闭时向用户显示一条消息,该功能在 Chrome 和 IE 上运行良好,但不适用于 Firefox,我使用的是 Firefox版本 26.0 我已经尝试了很多,但没有任何意义,有人说它是 Firefox 中的一个错误,就像在这个 post 和另一个人提出了一些解决方案,如post 我尝试了所有使用 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 Header 和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天全站免登陆