/** * 异步加载依赖的javascript文件 * src:script的路径 * callback:当外部的javascript文件被load的时候,执行的回调 */ function loadAsyncScript(src, callback) { var head = document.getElementsByTagName("head")[0]; var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute("src", src); script.setAttribute("async", true); script.setAttribute("defer", true); head.appendChild(script); //fuck ie! duck type if (document.all) { script.onreadystatechange = function() { var state = this.readyState; if (state === 'loaded' || state === 'complete') { callback(); } } } else { //firefox, chrome script.onload = function() { callback(); } } }