爲什麼腳本type =「text/plain」不是onload?下面的loadPlain不會觸發回調,但loadScript會。腳本或鏈接標記上的「type」或「rel」,「text/plain」的Javascript onload回調
我希望這工作......應該不是嗎?
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的,但那不是問題:)
你是如何使用你的函數loadPlain的?我問,因爲它在風格上與你的函數loadScript看起來很相似,所以它可能會以你使用它的方式導致問題。 – Trendy