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

时间:2012-01-09 13:01:26

标签: javascript caching safari back-button

单击后退按钮时,遇到装有旧YouTube视频的safari时遇到问题。我尝试将onunload =“”(此处提到Preventing cache on back-button in Safari 5)添加到body标签中,但在这种情况下它不起作用。

有没有办法阻止某个页面上的缓存加载safari?

8 个答案:

答案 0 :(得分:160)

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

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

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

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

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

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

答案 1 :(得分:6)

是的,Safari浏览器不像Firefox和Chrome那样处理后退/前进按钮缓存。特别是像vimeo或youtube视频这样的iframe几乎没有缓存,尽管有一个新的iframe.src。

我找到了三种处理方法。选择最适合您的情况。 在Firefox 53和Safari 10.1上测试的解决方案

<强> 1。检测用户是否正在使用后退/前进按钮,然后通过替换src来重新加载整个页面或仅重新加载缓存的iframe

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
        }

<强> 2。如果页面被缓存,则重新加载整个页面

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
        });

答案 2 :(得分:4)

所有这些答案都是一些黑客攻击。在现代浏览器(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'

答案 3 :(得分:1)

您可以使用锚点,并观察文档的位置href;

的值

http://acme.co/开始,向该位置添加内容,例如&#39;#b&#39;;

所以,现在你的网址是http://acme.co/#b,当一个人点击后退按钮时,它会回到http://acme.co,并且间隔检查功能会看到我们设置的哈希标记缺失,清除了interval,并使用附加时间戳的方式加载引用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>

这是未经测试的,但它应该以最小的调整工作。

答案 4 :(得分:0)

该行为与Safari的后退/转发缓存有关。您可以在相关的Apple文档中了解它:http://web.archive.org/web/20070612072521/http://developer.apple.com/internet/safari/faq.html#anchor5

Apple自己的修复建议是在您的网页上添加一个空的iframe:

<iframe style="height:0px;width:0px;visibility:hidden" src="about:blank">
    this frame prevents back forward cache
</iframe>

(之前接受的答案似乎也有效,只是想填写文档和其他可能的修复方法)

答案 5 :(得分:0)

首先在代码中插入字段:

<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();
        }
});

答案 6 :(得分:0)

有许多禁用bfcache的方法。最简单的方法是设置一个“卸载”处理程序。我认为让'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

答案 7 :(得分:0)

在使用3个不同的锚链接到下一页时,我遇到了同样的问题。从下一页返回并选择其他锚点时,链接没有更改。

所以我有

deriv(process_virtual_memory_bytes{job="$job", instance="$instance"}[$__interval])

更改为

<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>

在线找到的用于单独卸载,重新加载和重新加载(true)的解决方案均无效。希望这对遇到同样情况的人有所帮助。

相关问题