DOMにスクリプトタグを追加して複数のjavascript参照を動的にロードしています。動的にJavaScriptを読み込んでいます - 実行を待っていますか?
スクリプトの読み込み動作の説明に感謝します。
私は、DOMにスクリプト要素を追加すると、スクリプトがダウンロードされて実行されると仮定しています。しかし、.onload
イベントが発生したときは?スクリプトの実行が開始されるか、実行が終了するとすぐに? 2番目の場合、スクリプトの実行/初期化を待つ方法(動的にロードされた参照に依存するスクリプトを追加htmlに追加するコールバックを実行したいと思います)私は以下の機能を使用するスクリプトをロードするために
:
function recursiveInclude(scriptPathArray, callback) {
if (scriptPathArray.length > 0) {
var scriptPath = scriptPathArray[0];
// check if script is already loaded if not load
// this_loadedScriptList is one scope level up variable
if (this_loadedScriptList.indexOf(scriptPath) > 0 == false) {
var body = document.getElementsByTagName('body')[0];
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.src = scriptPath;
// first script from the array will loaded as soon as body.appendChild function will be called
// when script loads 'onload' event will fire next script loading
// to this work properly first script from scriptPathArray has to be removed:
scriptPathArray.shift();
// if there are any other scripts to load, load them sequentially
if (scriptPathArray.length > 0) {
// then bind the event to the callback function
// there are several events for cross browser compatibility
// script.onreadystatechange = callback;
scriptElement.onload = recursiveInclude(scriptPathArray, callback);
} else {
// if no other scripts to load - fire base callback;
scriptElement.onload = callback;
}
// fire the loading
body.appendChild(scriptElement);
// add script to loaded array.
this_loadedScriptList.push(scriptPath);
}
}
}
感謝を..だから.'onload'は、スクリプトの実行後に解雇されなければなりません?私は私のローディング機能をチェックする必要があります - 何か間違っている必要があります... –