我目前正在计算我的应用程序中打开的选项卡数量。但我的问题是,似乎我的脚本不会检测onload事件。这是我的代码。 我正在使用HTML5网络存储和原生js。我没有使用jQuery来了解更多关于本地js的知识。无法检测窗口是否加载
(function(w) {
function Tabz(win, key) {
this.name = '';
this.storageKey = key;
if(win.name != '')
this.name = win.name;
else {
var windowArr = JSON.parse(localStorage.getItem(key)) || [];
this.name = "tabz_"+ windowArr.length;
win.name = this.name;
windowArr.push(this.name);
localStorage.setItem(this.storageKey, JSON.stringify(windowArr));
}
}
Tabz.prototype.getStorage = function() {
return localStorage.getItem(this.storageKey);
}
Tabz.prototype.removeWindow = function() {
//remove window function here
}
var newWindow = new Tabz(w, 'counter');
window.load = function() {
var count = JSON.parse(newWindow.getStorage()).length;
alert(count!); // this wont execute so that I can check the count.
}
})(window);
可能是因为负载甚至发生在您的函数执行之前。既然这整件事情都是在页面加载的时候执行的,为什么不删除“window.load = function(){}”,并将警报计数放在主函数中。 – frenchie 2014-10-22 03:05:59