Safari 15 beforeunload shows exit alert but does not prevent navigation to cached pages.

In JavaScript we can use the beforeunload event to give users a chance to save unsaved work before they leave a page where they were editing some kind of document.


Chrome and Firefox implement this correctly: If the alert is shown, navigation is suspended until you respond. In Safari, closing the window correctly shows the alert and waits for user input. However, if the user tries to leave the page by navigating to a cached page via typing or clicking a link or clicking a bookmark, Safari displays the alert and simultaneously navigates to the requested page.

The result is Safari now shows another page while still displaying the beforeunload alert. Pressing Cancel/Leave buttons dismiss the alert but have no effect on navigation.


This is a confusing and frustrating bug.


I see this behavior on Safari 15 and 16 on my Macs.


Ben


Below is a screenshot of the alert from my test HTML page, now navigated to Apple.com. I had already just visited Apple.com so it's cached. If apple.com was not cached, Safari would have correctly blocked navigation.



MacBook Pro 15″, OS X 10.11

Posted on Sep 22, 2022 7:21 AM

Reply

Similar questions

1 reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Safari 15 beforeunload shows exit alert but does not prevent navigation to cached pages.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.