随笔 - 507  文章 - 0  评论 - 57 

JavaScript 之 动态加载JS代码或JS文件

 

2、动态加载JS文件

<script type="text/javascript">
function loadScript(url, callback) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  if(typeof(callback) != "undefined"){
     if (script.readyState) {
        script.onreadystatechange = function () {
        if (script.readyState == "loaded" || script.readyState == "complete") {
           script.onreadystatechange = null;
           callback();
        }
    };
  } else {
      script.onload = function () {
        callback();
      };
    }
  }
  script.src = url;
  document.body.appendChild(script);
}
loadScript("jquery-latest.js", function () { //加载,并执行回调函数
  alert($(window).height());
});
//loadScript("jquery-latest.js"); //加载js文件
</script>

 

posted on 2017-02-21 16:35  Now,DayBreak  阅读(100)  评论(0编辑  收藏