码迷,mamicode.com
首页 > 其他好文 > 详细

LoadScript

时间:2016-02-03 11:49:34      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

function loadScripts(urls, callback) {
  if (typeof (urls) === "string"){
    urls = [urls];
  }
  var returnCallback = function (url) {
    returnCallback.Counter++;
    if (returnCallback.Counter >= urls.length){
      callback();
    }
  }
  returnCallback.Counter = 0;
  var oHead = document.getElementsByTagName("head")[0];
  for (var i = 0; i < urls.length; i++){
    loadScript(urls[i], returnCallback,oHead);
  }
}

function loadScript(url, callback,oHead) {
  var script = document.createElement("script")
  script.type = "text/javascript";
  script.src = url;
  if (script.readyState) { //IE
    script.onreadystatechange = function () {
      if (script.readyState == "loaded" || script.readyState == "complete") {
        script.onreadystatechange = null;
        callback(url);
      }
    };
  }
  else { 
    script.onload = function () {
      callback(url);
    };
  }
  oHead = oHead || document.getElementsByTagName("head")[0];
  oHead.insertBefore( script, oHead.firstChild );
}

 

参考:http://www.cnblogs.com/aarond/category/504617.html

   http://www.cnblogs.com/sntetwt/p/4075154.html 

LoadScript

标签:

原文地址:http://www.cnblogs.com/mengff/p/5179300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!