1

I have one beforeunload handler, that is called when the user actually navigates away from the page:

$(window).on("beforeunload", function () {
    cleanup();
});

Another part of my application might however add another beforeunload handler, that asks the user if he really wants to leave because of an ongoing operation:

$(window).on("beforeunload", function () {
    return "Do you really want to leave";
});

How can I assure, that the second handler will always be called first and that the first handler will not be called in case the user decides to stay on the page?

I already tried to use the unload event instead. But this doesn't work since it will not execute my clean-up function and the backend call within that function reliably.

basilikum
  • 10,378
  • 5
  • 45
  • 58
  • 1
    Try this: http://stackoverflow.com/questions/17759703/is-there-any-way-to-detect-if-user-pressed-stay-on-page-or-leave-page-in-bef – Ismael Miguel Apr 14 '16 at 08:30
  • @IsmaelMiguel Thanks, but none of the answers in this questions works in my case. I can't use timeout since I have to do sth when the user leaves, not when he stays. Also, I can't use `unload` since it have to do backend call in my cleanup function and using `unload` this call will sometimes not be executed. – basilikum Apr 14 '16 at 08:39
  • You're right. Those requirements are actually different. Didn't noticed it. I'm really sorry. – Ismael Miguel Apr 14 '16 at 08:44

1 Answers1

1

You can use window.confirm in case. As in (pseudo-code)

$(window).on("beforeunload", function () {
 var result = window.confirm('Do you really want to quit?');
 if (result) {
  cleanup();
 }
}

This way, if the user wants to quit it will cleanup. Does that work?>

Jeremy Rajan
  • 662
  • 3
  • 12
  • Thank you but as I said, I have to different handlers, added by two different parts of my application and I would like to keep things seperated. So the question was, if I can somehow assure the order in which those handlers are called and prevent all following handlers if the user decides to stay during the dialog in one particular handler. – basilikum Apr 14 '16 at 08:45
  • So, by default on unload it will cleanup? And you want a separate handler for user navigating away, only if the user is in the process of doing something? – Jeremy Rajan Apr 14 '16 at 08:48
  • Yes, cleanup should be performed when the user actually leaves, so technically on unload. However, the unload event doesn't seem to be that reliable, so I need to use beforeunload. One part of my app might start audio recording and will ask the use if he really wants to leave, if he is in the middle of recording something. If he then decides to stay, to global clean-up should not be called. – basilikum Apr 14 '16 at 08:52
  • Actually, this doesn't seem to work anyway. I don't see a dialog if I use window.confirm within the beforeunload handler. – basilikum Apr 14 '16 at 09:08
  • Ok, let me take a look. I am still guessing, if we can change it to just one handler and then do things accordingly. Gimme a sec :) – Jeremy Rajan Apr 14 '16 at 09:10