当点击后退按钮时,Safari会加载旧的YouTube视频,我遇到了这个问题。我尝试在body标签中添加onunload =""(在这里提到:Preventing cache on back-button in Safari 5),但在这种情况下它不起作用。
有没有办法防止Safari在特定页面上从缓存中加载?
当点击后退按钮时,Safari会加载旧的YouTube视频,我遇到了这个问题。我尝试在body标签中添加onunload =""(在这里提到:Preventing cache on back-button in Safari 5),但在这种情况下它不起作用。
有没有办法防止Safari在特定页面上从缓存中加载?
onload
事件将不会被触发。相反,您可以检查onpageshow
事件的persisted
属性。在初始页面加载时,它被设置为false。当从回溯缓存中加载页面时,它被设置为true。window.onpageshow = function(event) {
if (event.persisted) {
window.location.reload()
}
};
如果您正在使用jQuery,请执行以下操作:
$(window).bind("pageshow", function(event) {
if (event.originalEvent.persisted) {
window.location.reload()
}
});
window.location = window.location
而不是window.location.reload()
,原因我认为已经被时间的迷雾所遗忘。 - Paul D. Waite所有这些答案都有点儿不正规。在现代浏览器(Safari)中,只有 onpageshow
解决方案有效。
window.onpageshow = function (event) {
if (event.persisted) {
window.location.reload();
}
};
但在较慢的设备上,有时您会在重新加载之前看到上一个缓存视图闪现。解决此问题的正确方法是在服务器响应中将Cache-Control设置为以下值:
'Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store'
Cache-Control: no-store
。请参考https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#preventing_storing。 - Nathan是的,Safari浏览器处理后退/前进按钮缓存与Firefox和Chrome不同。特别是像vimeo或youtube视频这样的iframes几乎没有被缓存,尽管有一个新的iframe.src。
我找到了三种方法来处理这个问题,请根据您的情况选择最好的解决方案。在Firefox 53和Safari 10.1上测试了这些解决方案。
1. 检测用户是否使用后退/前进按钮,然后重新加载整个页面或通过替换src仅重新加载已缓存的iframes
if (!!window.performance && window.performance.navigation.type === 2) {
// value 2 means "The page was accessed by navigating into the history"
console.log('Reloading');
//window.location.reload(); // reload whole page
$('iframe').attr('src', function (i, val) { return val; }); // reload only iframes
}
如果页面已被缓存,请重新加载整个页面
window.onpageshow = function (event) {
if (event.persisted) {
window.location.reload();
}
};
3. 从历史记录中删除页面,使用户不能通过后退/前进按钮再次访问该页面
$(function () {
//replace() does not keep the originating page in the session history,
document.location.replace("/Exercises#nocache"); // clear the last entry in the history and redirect to new url
});
我曾经遇到过相同的问题,使用了3个不同的锚点链接到下一页。当从下一页返回并选择不同的锚点时,链接没有改变。
所以我做了以下操作:
<a href="https://www.example.com/page-name/#house=house1">House 1</a>
<a href="https://www.example.com/page-name/#house=house2">View House 2</a>
<a href="https://www.example.com/page-name/#house=house3">View House 3</a>
改为
<a href="/page-name#house=house1">House 1</a>
<a href="/page-name#house=house2">View House 2</a>
<a href="/page-name#house=house3">View House 3</a>
也用于安全方面:
// Javascript
window.onpageshow = function(event) {
if (event.persisted) {
window.location.reload()
}
};
// JQuery
$(window).bind("pageshow", function(event) {
if (event.originalEvent.persisted) {
window.location.reload()
}
});
在网上找到的卸载、重新加载和单独重新加载(true)解决方案都没有起作用。希望这可以帮助到遇到同样情况的人。
<iframe style="height:0px;width:0px;visibility:hidden" src="about:blank">
this frame prevents back forward cache
</iframe>
之前被接受的答案也是有效的,我只是想加入文档和另一个潜在的解决方案。
http://acme.co/
开始,将一些内容附加到位置,例如“#b”;http://acme.co/#b
,当用户点击返回按钮时,它会返回到http://acme.co
,并且间隔检查函数会看到我们设置的哈希标签缺失,清除间隔,并加载附加了时间戳的引用URL。<script>
document.location.hash = "#b";
var referrer = document.referrer;
// setup an interval to watch for the removal of the hash tag
var hashcheck = setInterval(function(){
if(document.location.hash!="#b") {
// clear the interval
clearInterval(hashCheck);
var ticks = new Date().getTime();
// load the referring page with a timestamp at the end to avoid caching
document.location.href.replace(referrer+'?'+ticks);
}
},100);
</script>
这段代码尚未经过测试,但只需要最小限度的调整即可正常工作。
有很多方法可以禁用 bfcache。最简单的方法是设置一个“unload”处理程序。我认为让“unload”和“beforeunload”处理程序禁用 bfcache 是一个巨大的错误,但这就是他们所做的(如果你想要其中一个处理程序并且仍然使 bfcache 工作,你可以在 beforeunload 处理程序中删除 beforeunload 处理程序)。
window.addEventListener('unload', function() {})
在此处阅读更多:
https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/1.5/Using_Firefox_1.5_caching
首先在您的代码中插入字段:
<input id="reloadValue" type="hidden" name="reloadValue" value="" />
然后运行jQuery:
jQuery(document).ready(function()
{
var d = new Date();
d = d.getTime();
if (jQuery('#reloadValue').val().length == 0)
{
jQuery('#reloadValue').val(d);
jQuery('body').show();
}
else
{
jQuery('#reloadValue').val('');
location.reload();
}
});
window.addEventListener('pageshow', (event) => {
if (event.persisted) {
if(!navigator.userAgent.includes('Chrome') && navigator.userAgent.includes('Safari')){
window.location.reload();
}
}
});