这段js的作用是什么?异步加载么?

mia0x75 发布于 2016/06/01 22:57
阅读 415
收藏 0
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的方法,是不会主动加载任何内容吧?
加载中
0
前世疯狂
前世疯狂
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之后

0
mark35
mark35
没格式化的代码谁会看?
0
WolfX
WolfX
好乱啊, 动态生成了script和link标签
0
yak
yak
还可以加载css
0
阿信sxq
阿信sxq
不知道格式化代码吗,又一个伸手党
0
海水味儿
不格式,不管!
返回顶部
顶部