浏览器回退到上一页,刷新历史页面,清除缓存的方法

当用户通过浏览器上一页按钮返回历史页面时,一般浏览器都会直接读取缓存里的数据,哪怕页面设置了不缓存,如下:

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">

有时候也没有效果,这时我们可以通过刷新页面的方式来达到清空缓存的效果。亲测有效:

/*event.persisted 判断浏览器是否有缓存, 有为true, 没有为false*/
window.addEventListener('pageshow', function (event) {
    if (event.persisted || window.performance && window.performance.navigation.type == 2){
        location.reload();
    }
},false);

 

posted @ 2021-09-17 10:41  夜未央唐汉  阅读(863)  评论(0)    收藏  举报