js
複数のファイルを動的に追加したい。各ファイルには関数定義があり、そのコードをオブジェクトのプロパティに保存したいと思います。プレーンなJavaScriptとscript
タグを使用しています。しかし、onload
イベントは正しく機能しません。1つのファイルだけを含めると、すべてが正常に機能します。しかし、複数ある場合onload
は、故障しています。これが私のコードです、それはオブジェクトのメソッドです:
// My files I want to include:
one.js
function one() { alert('one') };
two.js
function two() { alert('two') };
// My code in the object
var one = null;
var two = null; // In these properties I want to save my functions
function inc(files) { // 'files' is the object {one: 'one', two: 'two'}
'one' and 'two' are names of function in my files
for (key in files) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
var fullPath = '/path/to/file/' + files[key] + '.js'; // /path/to/file/one.js, /path/to/file/two.js,
script.setAttribute('src', fullPath);
// And here's my eventhandler. After the script is loaded,
// names of my functions are in global scope. So, the task is:
// to take them and save in properties using setter.
script.onload = function() {
setProps(window[files[key]]); // setProp(window.one / window.two);
window[files[key]] = null; // remove them from global scope
alert(one/two); // to check if I have functions saved in properties
}
document.getElementsByTagName('head').item(0).appendChild(script);
};
};
最初に言ったように、1つのファイルをロードすればすべてが正常に機能します。しかし、複数の場合はonload
2回動作しますが、関数定義を使用した2番目のファイルに対してのみ機能し'two'
ます。Alertは2回発生し、のコードを表示します'two'
。'two'
そして、私は自分のプロパティの関数しか持っていません'two'
。これは、パラメータを使用して最後に渡した関数です。そして、ファイルはDOMだけのファイルに追加されます。
<script>
ループの外側でタグを作成しようとしfor/in
ました。配列を作成し、2つのスクリプトのそれぞれをその配列の個別の要素として保存しましたが、どちらも役に立ちません。最後のファイルonload
だけがと2回にあります。
何が問題ですか?2つのファイルで機能しないのはなぜですか?それを解決することは可能ですか?