Chrome中的脚本预加载失败

时间:2016-04-09 19:55:40

标签: javascript google-chrome

我正在尝试预加载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

1 个答案:

答案 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);