1、JQuery方法
$.getScript("./test.js"); //加载js文件 $.getScript("./test.js",function(){ //加载test.js,成功后,并执行回调函数 console.log("加载js文件"); });
2、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文件
转载地址:http://www.jb51.net/article/78432.htm