以下のスクリプトを作成しました。これは、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")
}
}