javascript - 使用 Page Visibility API 在页面隐藏时添加和删除临时哈希
问题描述
我找到了一篇中篇文章,描述了如何在 ios 上为独立的 Web 应用程序保留用户状态,或者在用户离线后继续工作。在 iOS 设备上,点击已安装的应用程序图标将强制重新加载目标 URL,从而为用户清除应用程序的当前状态。文章的链接是https://medium.com/@samthor/add-to-home-screen-apps-and-their-lifecycles-9ccd05f96e71
我在我所指的文章中包含了该部分的屏幕截图
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 应用程序保留用户的状态,但它不起作用。有人可以帮我吗?谢谢
解决方案
推荐阅读
- vue.js - :value 未使用 vue.js 在隐藏的输入字段上设置
- rest - API 和集成 (Marketo) 之间有什么区别
- reactjs - .addSubmitChangeListener 不是函数
- python - 如何检索雪花或任何此类数据库等数据库中的所有目录名称、模式名称和表名称?
- wordpress - Worpress 评论错误“您发布评论的速度太快了。放慢速度”
- android - 在 Android 应用程序中初始化 OneSignal 时出现问题 - 在 runtime_flags 中设置的未知位:0x8000
- docker - Portainer:使用 docker prune 的主机作业不起作用
- android - 带有 TCP 信息源的 Android 架构存储库
- android - 如果用作表达式,'if' 必须同时具有 main 和 'else' 分支
- java - 使用葡萄从 groovy 脚本运行主类