为什么不为 script type="text/plain" 触发 onload ?下面的 loadPlain 不会触发回调,但 loadScript 会。
我希望这能奏效……不应该吗?
loadPlain("http://localhost/ajax/libs/jquery/1.10.2/jquery.min.js", function(element) {
alert(1);
alert(element.innerHTML);
}, undefined, {})
function loadPlain(path, callback, errorCallback, options) {
var element = document.createElement('script');
element.setAttribute("type", 'text/plain');
element.setAttribute("src", path);
return loadBase(element, callback, errorCallback, options);
}
function loadScript(path, callback, errorCallback, options) {
var element = document.createElement('script');
element.setAttribute("type", 'text/javascript');
element.setAttribute("src", path);
return loadBase(element, callback, errorCallback, options);
}
function loadBase(element, callback, errorCallback, options) {
element.loaded = false;
if (element.readyState){ // IE
element.onreadystatechange = function(){
if (element.readyState == "loaded" || element.readyState == "complete"){
element.onreadystatechange = null;
loadBaseOnload(element, callback);
}
};
} else { // Others
element.onload = function() {
loadBaseOnload(element, callback);
};
}
element.onerror = function() {
errorCallback && errorCallback(element);
};
(options.elementAppendTo || document.head || loadBase.head || (loadBase.head = document.getElementsByTagName('head')[0]) || document.body).appendChild(element);
return element;
}
function loadBaseOnload(element, callback) {
if (element.loaded != true) {
element.loaded = true;
if ( callback ) callback(element);
}
}
请注意,我知道 XMLHttpRequest,但这不是问题 :)