首页 > 代码库 > 动态引入Js文件

动态引入Js文件

    var src = "http://www.mamicode.com/Scripts/Test.js";      $("<script type = ‘text/javascript‘ src=http://www.mamicode.com/‘" + src + "‘><\/script>").appendTo("head");  //动态加载    TestAlert123123();   //调用Test.js中的函数

 

二、顺序添加(QQ)

  <img src="http://www.mamicode.com/1.jpg" width="200" height="300" />     <script src="http://www.mamicode.com/jquery/jquery-1.4.1.js" type="text/javascript"></script>     <script src="http://www.mamicode.com/js/hello.js" type="text/javascript"></script>     <script src="http://www.mamicode.com/js/world.js" type="text/javascript"></script>     <script type="text/javascript">         function loadScript(url, callback) {             var script = document.createElement("script");             script.type = "text/javascript";              //IE             if (script.readyState) {                 script.onreadystatechange = function () {                     if (script.readyState == "loaded" || script.readyState == "complete") {                         script.onreadystatechange = null;                         callback();                     }                 }             } else {                 //非IE                 script.onload = function () {                     callback();                 }             }             script.src = url;             document.getElementById("head").appendChild(script);         }         //第一步加载jquery类库         loadScript("jquery/jquery-1.4.1.js", function () {             //第二步加载hello.js             loadScript("js/hello.js", function () {                 //第三步加载world.js                 loadScript("js/world.js", function () {                  });             });         });     </script>