动态加载JS/CSS文件
动态加载JS/CSS文件,可以判断是否加载完毕。
function loadScript(url, callback) {var script = document.createElement("script")script.type = "text/javascript";if (script.readyState) { // IEscript.onreadystatechange = function() {if (script.readyState == "loaded"|| script.readyState == "complete") {script.onreadystatechange = null;callback();}};} else { // Othersscript.onload = function() {callback();};}script.src = url;document.getElementsByTagName("head")[0].appendChild(script);}//例子loadScript("file1.js", function(){ alert("File is loaded!");});//顺序加载loadScript("file1.js", function() {loadScript("file2.js", function() {loadScript("file3.js", function() {alert("All files are loaded!");});});});