1

Given a webpage that sets the beforeunload event handler in the Safari browser and the development console is closed, the beforeunload handler cannot always prevent navigation away from the webpage. When the development console is open, the handler almost always can prevent navigation away from the page.

This may be reproduced in two ways.

Reproducible Method 1

Using a Safari Browser (Version 12.0.3 (13606.4.5.3.1)) visit the following index.html:

<!doctype html>
<html lang="en">
  <body>
    <script type="text/javascript">
      window.onbeforeunload = (evt) => { evt.preventDefault(); evt.returnValue = false; return true; };
    </script>
    <p>hello, world!</p>
  </body>
</html>

then, open the development console, select the navigation bar, type www.ibm.com and hit return. An alert will appear prompting the user to "Stay" or "Leave".

Close the console, retry the navigation, a prompt may or may not appear. If it does appear and is not clicked within 2-5 seconds ("Stay"), the new page will be visited although the prompt will remain. Clicking on the prompt has no apparent affect after that.

Reproducible Method 2

Using a Safari browser begin to answer a stackoverflow question. Open the development console, then using the navigation bar attempt to visit another site. An alert should appear guarding against loss of form data. Then, close the development console and re-attempt the navigation. This time, the navigation should go through and no alert will be displayed or, if too long before selecting "Stay", navigation will continue.

Discussion

  1. Can anyone else reproduce this?
  2. Advice on whether I should file a bug against Safari or Webkit is welcome.
  3. I've tested Method 1 on Firefox and did not see the same behavior - beforeunload acted mostly as expected.
  4. Have I failed to conjure the onbeforeunload function in just the correct way?
  5. I first saw this using Angular (with rxjs), however, as I've been able to reproduce it with straight HTML and in another website, this has nothing to do with Angular.

EDIT (2019-06-18)

Marked as possible duplicate of this question. It is not because that question explores a bug due to back button and bf history caching in Safari. This problem occurs without pressing the Back Button. If it is related somehow to the browser cache mentioned in the associated bug, someone will have to explain that to me, as I cannot see any obvious connection.

Community
  • 1
  • 1
Andrew Philips
  • 1,950
  • 18
  • 23
  • Possible duplicate of [Safari: onbeforeunload](https://stackoverflow.com/questions/40938707/safari-onbeforeunload) – Herohtar Jun 18 '19 at 19:24
  • @Herohtar thanks for the quick response. IMO, in a skim of the possible dup and its associated links, I don't think it's a dup, although there are some intriguing items referenced in there that I'd like to understand better. – Andrew Philips Jun 18 '19 at 19:30

0 Answers0