Android Chrome window.onunload [英] Android Chrome window.onunload
问题描述
我正在开发专门针对Android和Chrome 的HTML5应用。我的问题源于跟踪打开的浏览器选项卡的要求。我通过创建存储在每个选项卡的sessionStorage中的唯一ID来完成此操作。然后,我通过在每个选项卡有权访问的localStorage阵列中注册每个ID来跟踪打开的选项卡。
I am developing an HTML5 app specifically for Android and Chrome. The problem I have stems from the requirement to track open browser tabs. I do this by creating a unique ID stored in each tab's sessionStorage. I then track the open tabs by registering each ID in a localStorage array that each tab has access to.
问题在于,使用window.onunload事件关闭选项卡时,我无法从localStorage中删除ID。该代码在桌面版Chrome中运行正常,但我无法在Android中运行。
The problem is that I cannot remove the ID from localStorage when closing a tab by using the window.onunload event. The code works fine in desktop Chrome but I cannot get it working in 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事件。
This event will fire when reloading a page, which is fine, just not on closing. I have also tried using the pagehide event.
推荐答案
取决于浏览器。一些使用 .onunload
,一些使用 onbeforeunload
。
Depends on the browser. Some use .onunload
, some use onbeforeunload
.
最快的解决方案是
Quickest solution is
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));
});
测试姜饼,ICS&果冻豆使用原生的Android浏览器。
Tested on gingerbread, ICS & jelly bean using native android browser.
这篇关于Android Chrome window.onunload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!