0

我创建了以下脚本,结合我对JS的理解,以及对lazyload.js、head.js、yepnope.js等流行脚本的一些研究...

我需要它以非阻塞方式加载 javascript 文件,我需要它很短,要内联使用它,它需要是纯 javascript。它适用于 Chrome 和 Firefox,但它在 IE 9 及以下版本上中断(不尊重顺序),可能是什么问题?

lazyLoader = {
    load: function (scripts) {
        lazyLoader.nodes = []
        lazyLoader.queue = [scripts];
        for (i = 0; i < lazyLoader.queue[0].length; ++i) {
            var element = document.createElement("script");
            element.type = "text/javascript"
            element.src = lazyLoader.queue[0][i];
            element.async = false;
            element.defer = true;
            element.onload = function () {
                this.onload = null;
            }
            element.onreadystatechange = function() {
                if (this.readyState == "loaded" || this.readyState == "complete" ) {
                    this.onreadystatechange = null;
                    lazyLoader.loaded();
                    console.log ("The script ", this.src, " is ready!")
                }
            }
            lazyLoader.nodes.push(element) 
        }
        for (i = 0; i < lazyLoader.nodes.length; ++i) {
            console.log ("The script ", lazyLoader.nodes[i].src, " will be appended.")
            document.body.appendChild(lazyLoader.nodes[i])
        }
    },
    loaded: function() {
        console.log ("Loaded")
    }
}
4

1 回答 1

0

所以我发现了问题,我发布了整个注释代码:

smallLoader = {
    load: function (scripts) {
        // The queue for the scripts to be loaded
        smallLoader.queue = scripts;
        smallLoader.pendingScripts = [];
        // There will always be a script in the document, at least this very same script... 
        // ...this script  will be used to identify available properties, thus assess correct way to proceed
        var firstScript = document.scripts[0];
        // We will loop thru the scripts on the queue
        for (i = 0; i < smallLoader.queue.length; ++i) {
            // Evaluates if the async property is used by the browser
            if ('async' in firstScript ) {
                // Since src has to be defined after onreadystate change for IE, we organize all "element" steps together... 
                var element = document.createElement("script");
                element.type = "text/javascript"
                //... two more line of code than necessary but we add order and clarity
                // Define async as false, thus the scripts order will be respected
                element.async = false;
                element.src = smallLoader.queue[i];
                document.head.appendChild(element);
            }
            // Somebody who hates developers invented IE, so we deal with it as follows:
            // ... In IE<11 script objects (and other objects) have a property called readyState...
            // ... check the script object has said property (readyState) ... 
            // ... if true, Bingo! We have and IE! 
            else if (firstScript.readyState) {
                // How it works: IE will load the script even if not injected to the DOM...
                // ... we create an event listener, we then inject the scripts in sequential order
                // Create an script element
                var element = document.createElement("script");
                element.type = "text/javascript"
                // Add the scripts from the queue to the pending list in order
                smallLoader.pendingScripts.push(element)
                // Set an event listener for the script element 
                element.onreadystatechange = function() {
                    var pending;
                    // When the next script on the pending list has loaded proceed
                    if (smallLoader.pendingScripts[0].readyState == "loaded" || smallLoader.pendingScripts[0].readyState == "complete" ) {
                            // Remove the script we just loaded from the pending list
                            pending = smallLoader.pendingScripts.shift()
                            // Clear the listener
                            element.onreadystatechange = null;
                            // Inject the script to the DOM, we don't use appendChild as it might break on IE
                            firstScript.parentNode.insertBefore(pending, firstScript);
                    }
                }
                // Once we have set the listener we set the script object's src
                element.src = smallLoader.queue[i];
            }
        }
    }
}
于 2013-08-09T16:14:23.293 回答