首页 > 解决方案 > 使用 Page Visibility API 在页面隐藏时添加和删除临时哈希

问题描述

我找到了一篇中篇文章,描述了如何在 ios 上为独立的 Web 应用程序保留用户状态,或者在用户离线后继续工作。在 iOS 设备上,点击已安装的应用程序图标将强制重新加载目标 URL,从而为用户清除应用程序的当前状态。文章的链接是https://medium.com/@samthor/add-to-home-screen-apps-and-their-lifecycles-9ccd05f96e71

我在我所指的文章中包含了该部分的屏幕截图

截图 1

截图2

function visibilityHandler() {
  var hash = '#bg';
  if (document.hidden && !window.location.hash) {
    window.history.replaceState(null, null, window.location + hash);
  } else if (!document.hidden && window.location.hash == hash) {
    var l = '' + window.location;
    window.history.replaceState(null, null, l.substr(0, l.length - hash.length));
  }
};
document.addEventListener('visibilitychange', visibilityHandler, false);
visibilityHandler();

此代码应该为独立的 Web 应用程序保留用户的状态,但它不起作用。有人可以帮我吗?谢谢

标签: javascriptprogressive-web-appsiphone-standalone-web-appios-standalone-mode

解决方案


推荐阅读