我目前正在计算应用程序中打开的选项卡的数量。但是我的问题是我的脚本似乎无法检测到事件加载。这是我的代码。我正在使用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() {
这会将load
属性添加到窗口,而不添加事件侦听器。我想你在找onload
。
window.onload = function() {
顺便说一句,使用事件属性被认为是不好的做法。使用addEventListener会更好。
window.addEventListener("load", function(){
//Do stuff...
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句