前端异步加载js且判断js是不是加载完成
程序员文章站
2022-07-15 09:13:40
...
/** * 异步加载依赖的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(); } } }
上一篇: ubuntu 安装 emacs24
下一篇: 一点学习感悟