Preventing cache on back-button in Safari 5

大憨熊 提交于 2019-11-26 20:27:01

The empty unload handler will not work anymore. 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 solution is to force a reload when page is loaded from bfcache.

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

If you are using jQuery then do:

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

After some googeling and digging I've found a solution, though I'm not too happy about it. Setting onunload="" in the body-tag causes Safari to invalidate the page and reload it uppon window.history.back();.

Here's another way:

    function invalidateBackCache() {
        // necessary for Safari: mobile & desktop
    }

    window.addEventListener("unload", invalidateBackCache, false);

I chose to go this route because adding HTML (onunload="") to the body tag in .Net involved modifying three files in my case. Setting the onunload attribute in jQuery didn't solve it either.

This works for mobile Sarfari (iPad) as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!