我正在开发一个 HTML5 应用程序专门针对 Android 和 Chrome。我遇到的问题源于跟踪打开的浏览器选项卡的要求。我通过创建存储在每个选项卡的 sessionStorage 中的唯一 ID 来实现此目的。然后,我通过在每个选项卡有权访问的 localStorage 数组中注册每个 ID 来跟踪打开的选项卡。
问题是,当使用 window.onunload 事件关闭选项卡时,我无法从 localStorage 中删除 ID。该代码在桌面 Chrome 中运行良好,但无法在 Android 中运行。
$(window).on('beforeunload', function () {
removeWindowGUID();
});
function removeWindowGUID() {
var guid = sessionStorage.getItem("WindowGUID");
var tmp = JSON.parse(localStorage.getItem("WindowGUIDs"));
tmp = tmp.remove(guid); // remove is a custom prototype fn
localStorage.setItem("WindowGUIDs", JSON.stringify(tmp));
}
重新加载页面时会触发此事件,这很好,只是在关闭时不会触发。
我也尝试过使用 pagehide 事件。
取决于浏览器。一些使用.onunload
, 一些使用onbeforeunload
.
最快的解决方案是
window.onunload = window.onbeforeunload = function() {
var guid = sessionStorage.getItem("WindowGUID");
var tmp = JSON.parse(localStorage.getItem("WindowGUIDs"));
tmp = tmp.remove(guid); // remove is a custom prototype fn
localStorage.setItem("WindowGUIDs", JSON.stringify(tmp));
});
使用本机 Android 浏览器在姜饼、ICS 和果冻豆上进行了测试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)