0

I have a page with an unload listener, something very simple like:

$(window).on("beforeunload", function(e){
    if (someCondition) return null;
});

After the load, if I click anywhere on the page then try to close my tab, I'll get a warning.

However, if I close the tab before any interaction with the DOM then I won't get the warning.

I put a breakpoint on the return null; that shows that it is called but then ignored by the browser (Chrome 67.0.3396.99 on Mac OS 10.13.6).

Is there a reason for that? Any way around it?

Thanks in advance for your help :)

Arnaud
  • 750
  • 10
  • 25
  • 2
    This is mainly intended to warn users about leaving pages with unsaved input ... and if you don’t interact with the DOM at all, then you likely have not made that much user input on the page either at this point, right? – CBroe Jul 24 '18 at 11:22
  • https://stackoverflow.com/questions/38879742/is-it-possible-to-display-a-custom-message-in-the-beforeunload-popup – Lain Jul 24 '18 at 11:23
  • @CBroe the context is: the user saves a form, that form contains an error detected on the server-side, I redirect to the page where I display an error message (explaning that the changes weren't saved) and the form again. I don't want people to accidentally close the tab thinking that their changes were saved if they didn't read the message carefully. – Arnaud Jul 24 '18 at 11:25
  • My guess, this is likely intended to prevent shady “advertisers” from trying to keep me on a page that I have no interest in whatsoever. // Perhaps you could perform your validity check via AJAX, then I would be staying on a page I have _already_ interacted with, so this limitation should not be an issue any more then …? – CBroe Jul 24 '18 at 11:28
  • Thant makes sense yes. Thanks a lot @CBroe. – Arnaud Jul 24 '18 at 11:29

0 Answers0