js动态加载 以及确定加载完成

利用js动态加载js文件到页面,并在确定加载完成后调用相关function 代码如下:

 1 var otherJScipt = document.createElement("script");
2 otherJScipt = document.createElement("script");
3 otherJScipt.setAttribute("type", "text/javascript");
4 otherJScipt.setAttribute("src", "/xxx.js");
5 document.getElementsByTagName("head")[0].appendChild(otherJScipt);//追加到head标签内
6
7
8 //判断服务器
9 if (navigator.userAgent.indexOf("IE") >= 0) {
10 //IE下的事件
11 otherJScipt.onreadystatechange = function () {
12 //IE下的判断,判断是否加载完成
13 if (otherJScipt && (otherJScipt.readyState == "loaded" || otherJScipt.readyState == "complete")) {
14 otherJScipt.onreadystatechange = null;
15 callMyFun();
16 }
17 };
18 }
19 else {
20 otherJScipt.onload = function () {
21 otherJScipt.onload = null;
22 callMyFun();
23 };
24 }

  

原文地址:https://www.cnblogs.com/vnii/p/2120798.html