8

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165

4 Answers4

15

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;
});
tharkay
  • 5,913
  • 2
  • 26
  • 33
faisale
  • 1,435
  • 14
  • 14
  • 1
    although this works, the confirmation message is not showing on firefox. off to look for way to do this... – Jim Oct 29 '14 at 03:35
  • it's firfox behavior @Jim, firefox doesn't allow to show custom message. – faisale Nov 02 '14 at 20:28
  • @faisale i dont want to shoq confirmation msg, how can i achieve this? – sadia Nov 17 '16 at 12:38
  • I don't get it, above code is to show confirmation message, if you don't want to show it then commented out the above code in you html. – faisale Oct 11 '17 at 10:23
5

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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
dharmx
  • 694
  • 1
  • 13
  • 20
  • Note that in FF 115 it worked only if the previous page had different origin. If the origin is same in both previous and next page (say localhost:3000 to localhost:3000), you won't see the message while going back. – 15 Volts Jun 13 '23 at 10:35
4

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

vince4128
  • 61
  • 4
-1

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;
Boris Zbarsky
  • 34,758
  • 5
  • 52
  • 55
  • 1
    Running the handler works fine. Posing dialogs from beforeunload handlers (apart from by returning false) is no longer allowed because too many sites were abusing that. – Boris Zbarsky Oct 06 '14 at 23:52