我创建了以下脚本,结合了我对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")
}
}
答案 0 :(得分: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];
}
}
}
}