移动端页面返回强制刷新页面

返回不刷新是因为,移动端浏览器为了节省流量一般返回都会去缓存,可以通过页面显示事件监听页面返回,通过event.persisted字段来判断当前页面是否是从缓存中获去的

window.addEventListener('pageshow', function (event) {
//event.persisted属性为true时,表示当前文档是从往返缓存中获取
  if (event.persisted) location.reload();
 });

实际测试发现,返回时页面显示事件虽然执行,但是location.reload()并没有效果,页面仍然没有刷新,尝试通过元控制当前页面不缓存,仍然失败。

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

华为尝试发现计时器刷Nova4e不兼容,其他浏览器外部其他ok
最终代码

function reload(){
    setTimeout(function() {
        location.reload()
    }, 500);
}

window.addEventListener('pageshow', function (event) {
    //event.persisted属性为true时,表示当前文档是从往返缓存中获取
    if (event.persisted) {
        reload();
    }
});

注:以上方案来自团队小伙伴贡献,记录下来,方便以后查阅

猜你喜欢

转载自www.cnblogs.com/HYZhou2018/p/12312913.html
今日推荐