Mozilla firefox not working with window.onbeforeunload

依然范特西╮ 提交于 2019-12-28 02:11:11

问题


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>

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.


回答1:


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



回答2:


I looked for a Firefox onbeforeunload solution over 2 days and with no luck, so I worked hard on this, and did it with a little trick.

In my trick, the user needs to click on the browser window at least once. If the user clicks on the window and then clicks on the back button, refreshes the page, or tries to exit the page, onbeforeunload events will fire,

$(window).on('beforeunload', function () {
    return "Are you sure you want to exit this page?";
});
$(window).one('click',function(){
    alert('hi');
    $('.javavoid').trigger('click');
});
.hide{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <a class="hide javavoid" href="javascript:void(0)">asdf</a>
    <p>Welcome, click anywhere then click on back button or refresh page</p>
</body>



回答3:


Using the code given in the MDN works for me in firefox/chrome/IE11 (haven't try other browser for now).

window.onbeforeunload = function (e) {
  var e = e || window.event;

  // For IE and Firefox
  if (e) {
    e.returnValue = 'Any string';
  }

  // For Safari
  return 'Any string';
};

here is the doc : Mdn window.onbeforeunload doc




回答4:


Why are you doing browser-sniffing here? The "Firefox" branch of your code is returning the return value of window.confirm, which is a boolean. But a boolean isn't callable, so assigning it to window.onbeforeunload is the same as assigning null.

What you probably want to do is remove the browser-sniffing and just do:

window.onbeforeunload = confirmExamClose;


来源:https://stackoverflow.com/questions/20773306/mozilla-firefox-not-working-with-window-onbeforeunload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!