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


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

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM