以前に含まれていなかった場合にのみjavascriptを含めるための小さなjavascriptスニペットを見つけました。
これは私自身のスクリプトでは機能しますが、2つのサードパーティ製ライブラリでは機能せず、その理由はよくわかりません。
var included_files = new Array();
function include_once(script_filename) {
if (!in_array(script_filename, included_files)) {
included_files[included_files.length] = script_filename;
include_dom(script_filename);
}
}
function in_array(needle, haystack) {
for (var i = 0; i < haystack.length; i++) {
if (haystack[i] == needle) {
return true;
}
}
return false;
}
function include_dom(script_filename) {
var html_doc = document.getElementsByTagName('head').item(0);
var js = document.createElement('script');
js.setAttribute('language', 'javascript');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', script_filename);
html_doc.appendChild(js);
return false;
}
function loaded() {
include_once("shared/scripts/jquery.min.js");
include_once("shared/scripts/iscroll.js");
$(document).ready(function () {
alert("hello");
});
}
エラー:$が定義されていません。jQueryを通常の方法でインポートし、「iScroll」が定義されていないと表示された場合(後で使用するため)。
何か案は?