且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单击后退按钮时防止从缓存加载 safari

更新时间:2023-11-19 16:12:58

您的问题是由 back-转发缓存.当用户导航离开时,它应该保存页面的完整状态.当用户使用后退按钮向后导航时,可以非常快速地从缓存中加载页面.这与仅缓存 HTML 代码的普通缓存不同.

Your problem is caused by back-forward cache. It is supposed to save complete state of page when user navigates away. When user navigates back with back button page can be loaded from cache very quickly. This is different from normal cache which only caches HTML code.

当页面为 bfcache 加载时 onload 事件不会被触发.相反,您可以检查 onpageshow 事件的 persisted 属性.它在初始页面加载时设置为 false.当页面从 bfcache 加载时,它被设置为 true.

When page is loaded for bfcache onload event wont be triggered. Instead you can check the persisted property of the onpageshow event. It is set to false on initial page load. When page is loaded from bfcache it is set to true.

Kludgish 解决方案是在从 bfcache 加载页面时强制重新加载.

Kludgish solution is to force a reload when page is loaded from bfcache.

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

如果您使用 jQuery,请执行以下操作:

If you are using jQuery then do:

$(window).bind("pageshow", function(event) {
    if (event.originalEvent.persisted) {
        window.location.reload() 
    }
});