简体   繁体   English

在 Safari 上刷新页面后,beforeunload 事件未触发

[英]beforeunload event not firing after page refresh on Safari

I'm trying to add a warning to a user if they are leaving the page without saving their changes, currently this works by setting the isEdited and isSubmitButtonClicked variables on certain events, Let's assume that isEdited is True and isSubmitButtonClicked is false.我正在尝试向用户添加警告,如果他们在没有保存更改的情况下离开页面,目前这是通过在某些事件上设置isEditedisSubmitButtonClicked变量来工作的,让我们假设isEdited为 True 并且isSubmitButtonClicked为假。

This works for Chrome/Firefox in all cases yet in Safari it works the first time, wherein the alert shows up, I can click leave page and then navigate back to that same page try to make another change and leave the page and this time the alert does NOT show up.这在所有情况下都适用于 Chrome/Firefox,但在 Safari 中它第一次工作,其中显示警报,我可以单击离开页面,然后导航回同一页面尝试进行另一次更改并离开该页面,这一次警报没有出现。

This is only an issue for Safari, does anyone have any experience or ideas I can try?这只是 Safari 的一个问题,有没有人有任何我可以尝试的经验或想法?

$(window).bind('beforeunload', function (e) {
    if (isEdited && !isSubmitButtonClicked) {
        var confirmationMessage = 'test';
        (e || window.event).returnValue = confirmationMessage;
        return confirmationMessage;
    }
    isSubmitButtonClicked = false;
});

我似乎记得一些关于 beforeunload 的事情,除非用户首先点击浏览器窗口,否则它不会触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM