js异步加载和按需加载

function loadScript(url,callback){
  var script = document.creatElement("script");
  script.type="text/javascript";
  if(script.readyState){
    script.onreadystateChange = function(){
      if(script.readyState == "complete" || script.readyState=='loaded'){
        callback();
      }
    }
  }else{
    script.onload=function(){
    callback();
   }
  }
  script.src = url;
  document.head.appendChild(script);
}
loadScript('test.js',test);

posted @ 2018-03-02 16:13  徐念念  阅读(158)  评论(0编辑  收藏  举报