0

There are several similar questions on here already but none of them provide a solution to what I'm looking for here.

When a user clicks the close button on their browser I need to pop up an alert to confirm that they really want to close their browser. This is easy enough to write:

$(window).bind('beforeunload', function(){
    return 'Are you sure you want to close your browser?';
});

The problem with this is that it also fires when you do things like refresh your browser, click on buttons and links, etc.

Most of these can be prevented by detecting the key presses and checking the keyCodes like this:

  if (e.keyCode == 114 || e.keyCode == 116 || e.keyCode == 0 || e.keyCode == 17 ||(e.ctrlKey && e.keyCode == 114)){
      confirmBrowserClose = false;
  }

  $("a").bind("click", function() {
    confirmBrowserClose = false;
  });

  $("form").bind("submit", function() {
    confirmBrowserClose = false;
  });

  $("input[type=submit]").bind("click", function() {
    confirmBrowserClose = false;
  });

These things prevent most of them but one thing it doesn't work for is refresh. I can prevent it from firing when the user refreshes using the keyboard (like F5) but I need to know how to prevent my confirmation alert from firing when the user clicks the refresh button or enter in the URL window.

Most of what I've found scattered around the internet says that it either can't be done or they talk about things like using the keyCodes and F5 refresh. I know this can be done because there are many sites that have this functionality working. A couple sites that are using it are Facebook and JSFiddle.net. In Facebook, if you start typing a status update and then try to close your browser, it will popup a confirmation. In JSFiddle, if you make changes to your fiddle and then try to close your browser it will pop up a warning alert that your changes will be lost if you close.

Does anyone know how to do this?

ddogg
  • 3
  • 4
  • 1
    You are confused! The confirmation can be implemented, but not separately for Close and Refresh. Even on Facebook and JSFiddle, doesn't matter if you Close or Refresh the browser, it will still prompt you. And as the rest of the internet says, this can't be done the way you want it. – bPratik Dec 10 '13 at 21:48
  • @bPratik is right. You cannot track any events outside of your document (e.g. clicking the refresh button) with JavaScript – Horen Dec 10 '13 at 21:52
  • That's not true though. The confirmation message that you get when you click to close the browser is different than the one you get when you click to reload the page - on both of those sites. So, they know which event it is somehow because they specify close or refresh in the alert message. – ddogg Dec 10 '13 at 21:54
  • `Facebook` and `JSFiddle` also pop-up the confirmation message on `close` as well as `reload` and click on external `links`. – Nagendra Rao Dec 10 '13 at 21:57
  • @ddogg - You are mistaken again. The confirmation message is the same for Close and Refresh. It is different if you click any link on the page that navigates away from it. This is achieved by adding an OnBeforeUnload event handler to catch the Close/Refresh, and then attaching a handler to the click of every link to warn the user with a more fancier dialog. Go check. :) So, like I said, they have no control over the former dialog as it is a responsibility of the browser (for security reasons), while the second dialog is theirs to do what they please! – bPratik Dec 10 '13 at 21:58
  • Thanks bPratik. Now I see that I was indeed confused. It would have been nice to be able to do this but if it's not possible, it's not possible... – ddogg Dec 10 '13 at 22:17
  • http://stackoverflow.com/questions/18457797/how-to-know-whether-refresh-button-or-browser-back-button-is-clicked-in-firefox if this (answer by Vipin Malik) works you could unbind the beforeunload-event if refresh is clicked. At least in Firebug it looked like it should be working. – Mister Henson Dec 10 '13 at 23:07

1 Answers1

0

This is not possible. If you observe properly, even Facebook.com and JSFiddle.net do not have this feature.

beforeunload is the only event that gets triggered on leaving the page.(either by page refresh [f5, ctrl+r], tab close, load another link[this also accounts to leaving the current page, hence triggering beforeunload])

Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92