解决浏览器回退不刷新页面问题

1.
window.addEventListener('pageshow', (e) => {
  console.log("打印e:", e, e.persisted);
  console.log(navigator.userAgent);
  if (e.persisted) {
    console.log("刷新页面");
    location.reload();
  }
});
2.
// 兼容浏览器(如vivo浏览器等)的页面隐藏
let hiddenProperty = 'hidden' in document ? 'hidden' :
'webkitHidden' in document ? 'webkitHidden' :
'mozHidden' in document ? 'mozHidden' :
null;

 

let visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
let onVisibilityChange = function () {
if (!document[hiddenProperty]) {
  location.reload();
  } else {
    console.log('页面非激活');
  }
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);
posted @ 2021-08-02 16:50  kimsaerono  阅读(484)  评论(0编辑  收藏  举报