顺序Javascript加载程序不遵守顺序
结合我对JS的理解,以及对lazyload.JS、head.JS、yepnope.JS等流行脚本的研究,我创建了以下脚本 我需要它以非阻塞的方式加载javascript文件,我需要它简短,内联使用,需要纯javascript。它可以在Chrome和Firefox上运行,但在IE 9上出现故障(不尊重顺序),这会是什么问题顺序Javascript加载程序不遵守顺序,javascript,internet-explorer,asynchronous,loader,Javascript,Internet Explorer,Asynchronous,Loader,结合我对JS的理解,以及对lazyload.JS、head.JS、yepnope.JS等流行脚本的研究,我创建了以下脚本 我需要它以非阻塞的方式加载javascript文件,我需要它简短,内联使用,需要纯javascript。它可以在Chrome和Firefox上运行,但在IE 9上出现故障(不尊重顺序),这会是什么问题 lazyLoader = { load: function (scripts) { lazyLoader.nodes = [] lazy
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")
}
}
lazyLoader={
加载:函数(脚本){
lazyLoader.nodes=[]
lazyLoader.queue=[scripts];
对于(i=0;i
所以我解决了问题,我发布了完整的注释代码:
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];
}
}
}
}
smallLoader={
加载:函数(脚本){
//要加载的脚本的队列
smallLoader.queue=脚本;
smallLoader.PendingScript=[];
//文档中总会有一个脚本,至少这个脚本是一样的。。。
//…此脚本将用于识别可用属性,从而评估正确的操作方法
var firstScript=document.scripts[0];
//我们将循环遍历队列上的脚本
对于(i=0;itrue
,我认为您不应该依赖按顺序加载的脚本。您是对的,设置“延迟”属性是一个疏忽。但是,即使没有“延迟”设置,脚本也无法按预期工作。