var Base = { head: document.getElementsByTagName("head")[0] || document.documentElement, Myload: function (B, A) { this.done = false; B.onload = B.onreadystatechange = function () { if (!this.done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) { this.done = true; A(); B.onload = B.onreadystatechange = null; if (this.head && B.parentNode) { this.head.removeChild(B) } } } }, getScript: function (A, C) { var B = function () { }; if (C != undefined) { B = C } var D = document.createElement("script"); D.setAttribute("language", "javascript"); D.setAttribute("type", "text/javascript"); D.setAttribute("src", A); this.head.appendChild(D); this.Myload(D, B) }, getStyle: function (A, CB) { var B = function () { }; if (CB != undefined) { B = CB } var C = document.createElement("link"); C.setAttribute("type", "text/css"); C.setAttribute("rel", "stylesheet"); C.setAttribute("href", A); this.head.appendChild(C); this.Myload(C, B) } }
function GetVerNum() { var D = new Date(); return D.getFullYear().toString().substring(2, 4) + '.' + (D.getMonth() + 1) + '.' + D.getDate() + '.' + D.getHours() + '.' + (D.getMinutes() < 10 ? '0' : D.getMinutes().toString().substring(0, 1)) }
Base.getScript('/statics/js/B.js?v=' + GetVerNum());
不调用Base的方法,是不会主动加载任何内容吧?
var Base = {
head: document.getElementsByTagName("head")[0] || document.documentElement,
Myload: function(B, A) {
this.done = false;
B.onload = B.onreadystatechange = function() {
if (!this.done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
this.done = true;
A();
B.onload = B.onreadystatechange = null;
if (this.head && B.parentNode) {
this.head.removeChild(B)
}
}
}
},
getScript: function(A, C) {
var B = function() {};
if (C != undefined) {
B = C
}
var D = document.createElement("script");
D.setAttribute("language", "javascript");
D.setAttribute("type", "text/javascript");
D.setAttribute("src", A);
this.head.appendChild(D);
this.Myload(D, B)
},
getStyle: function(A, CB) {
var B = function() {};
if (CB != undefined) {
B = CB
}
var C = document.createElement("link");
C.setAttribute("type", "text/css");
C.setAttribute("rel", "stylesheet");
C.setAttribute("href", A);
this.head.appendChild(C);
this.Myload(C, B)
}
}
定义了一个可以动态生成script标签和link标签的对象
GetVerNum() 功能类似于生成随机数时间戳
Base.getScript('/statics/js/B.js?v=' + GetVerNum())调用了Base.getScript(),并传入了参数A
getScript()运行时在head中的script中加载了地址为/statics/js/B.js的js文件后调用了Myload()
B.onload = B.onreadystatechange = function() {
if (!this.done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
this.done = true;
A();
B.onload = B.onreadystatechange = null;
if (this.head && B.parentNode) {
this.head.removeChild(B)
}
}
}
此时B为document.createElement("script"),该方法的调用绑定了当页面状态改变时移除head中的script标签的事件
在script标签中appendChild应该还是一个阻塞的过程吧,只是这个加载过程发生在页面onload之后
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。