最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Safari: onbeforeunload - Stack Overflow

programmeradmin2浏览0评论

Precondition: You should be on Safari 10.

Hi, I'm have trouble getting the Leave | Stay confirmation box to show up on my page across different browsers.

Please go to , and login with following credentials:

username: [email protected]
password: skl@0!_~!(

After you've logged in, go to the /wele page. Please click Settings in the left sidebar and go to the Membership tab.

If you can see "Enable Membership Features", then click it and change anything there and navigate to another page. You will see the confirmation box show up the first time you try to leave the page.

Click "Leave" to go to your target page, and then e back to this Settings page and try the same action; you won't see the confirmation box again (when using Safari).

I used this code snippet at: .js?v=68.160720

/* settingsPages.isChanged() is my custom function */
window.addEventListener("beforeunload", function(event) {
    if (settingsPage.isChanged()) {
        event.returnValue = "some string";
        return "some string";
    }
});

It works fine on Google Chrome and Firefox, but not on Safari.

Thanks!

Precondition: You should be on Safari 10.

Hi, I'm have trouble getting the Leave | Stay confirmation box to show up on my page across different browsers.

Please go to https://www.biznessapps./cms, and login with following credentials:

username: [email protected]
password: skl@0!_~!(

After you've logged in, go to the /wele page. Please click Settings in the left sidebar and go to the Membership tab.

If you can see "Enable Membership Features", then click it and change anything there and navigate to another page. You will see the confirmation box show up the first time you try to leave the page.

Click "Leave" to go to your target page, and then e back to this Settings page and try the same action; you won't see the confirmation box again (when using Safari).

I used this code snippet at: https://www.biznessapps./cms/v2/public/scripts/pages/settings.js?v=68.160720

/* settingsPages.isChanged() is my custom function */
window.addEventListener("beforeunload", function(event) {
    if (settingsPage.isChanged()) {
        event.returnValue = "some string";
        return "some string";
    }
});

It works fine on Google Chrome and Firefox, but not on Safari.

Thanks!

Share Improve this question edited Dec 6, 2016 at 12:48 Paul Z. asked Dec 2, 2016 at 18:24 Paul Z.Paul Z. 9052 gold badges10 silver badges28 bronze badges 8
  • @zerk It should be "After you've logged in, you will be redirected to the /wele page." – Paul Z. Commented Dec 2, 2016 at 21:58
  • Possible duplicate of window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera? – gantoine Commented Dec 3, 2016 at 4:48
  • have you found a solution? – Dario Rusignuolo Commented Dec 7, 2016 at 9:38
  • 1 @dario No, I could't find. This question is never duplicate of the article GAntoine mentioned. – Paul Z. Commented Dec 7, 2016 at 10:40
  • I opened another question, just in case... stackoverflow./questions/41014003/… – Dario Rusignuolo Commented Dec 7, 2016 at 10:43
 |  Show 3 more ments

1 Answer 1

Reset to default 3

This is likely due to Safari's Back-Forward cache. A decent article on this can be found here: https://team.goodeggs./you-forgot-about-bfcache-f7a9bdeceb6c#.qgri9etsb

The fix is to basically just reload the page if it is loaded from the cache, like so:

window.onpageshow = function(event) {
    if (event.persisted) {
        window.location.reload() 
    }
};

event.persisted is the magic part. It is true if the page was loaded using the browsers "back" or "forward".

发布评论

评论列表(0)

  1. 暂无评论