我正在尝试预加载js
文件而不执行它们。我的代码是
for(var i=0; i<preload.length; i++){
o = document.createElement('Object');
o.data = preload[i];
o.width = 1;
o.height = 1;
o.style.visibility = "hidden";
o.type = "text/cache";
o.className = "hidden";
console.log("Creating: ",o);
o.onload = function(){
console.info("Trying to load ",this.data," : ",i);
itemloaded(this);
}
// all others require body
document.body.appendChild(o);
}
这在firefox和IE中完美运行,但在Chrome中只触发了Creating
的日志,显示o
是一个匿名函数,而FF和IE则将其报告为Object
,Chrome似乎永远不会触发onload
答案 0 :(得分:-1)
为什么不使用commonjs或amd? 或试试这个。
var script = document.createElement('script');
script.src = 'http://path/to/your/script.js';
script.onload = function() {
// do something here
}
document.head.appendChild(script);