单击后退按钮时防止从缓存加载 safari [英] Prevent safari loading from cache when back button is clicked

查看:38
本文介绍了单击后退按钮时防止从缓存加载 safari的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单击后退按钮时 safari 加载旧的 youtube 视频时出现问题.我试过添加 onunload=""(这里提到 防止缓存在后面-Safari 5 中的按钮)添加到 body 标签,但在这种情况下不起作用.

Got an issue with safari loading old youtube videos when back button is clicked. I have tried adding onunload="" (mentioned here Preventing cache on back-button in Safari 5) to the body tag but it doesn't work in this case.

有什么办法可以防止从某个页面的缓存加载 safari 吗?

Is there any way to prevent safari loading from cache on a certain page?

推荐答案

您的问题是由 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() 
    }
});

这篇关于单击后退按钮时防止从缓存加载 safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆