如何使用AJAX加载外部javascript文件并知道加载失败?
我编写了一个函数,它将URL字符串数组作为第一个参数,并尝试使用它们加载外部脚本。这允许我在一个镜像关闭的情况下链接多个源如何使用AJAX加载外部javascript文件并知道加载失败?,javascript,ajax,cors,Javascript,Ajax,Cors,我编写了一个函数,它将URL字符串数组作为第一个参数,并尝试使用它们加载外部脚本。这允许我在一个镜像关闭的情况下链接多个源 function loadScript(scripts, name, index) { //Convert single string to array - not effective but easy if(!(scripts instanceof Array)) scripts = [scripts]; //Use first script src
function loadScript(scripts, name, index) {
//Convert single string to array - not effective but easy
if(!(scripts instanceof Array))
scripts = [scripts];
//Use first script src if no name is defined
if(name==null) {
name = scripts[0];
}
//Default index is 0
if(index==null)
index = 0;
//In case wrong index was provided
if(index>=scripts.length)
throw new Error("Aray index out of bounds.");
//Create request
var req = new XMLHttpRequest();
req.open("GET", scripts[index]);
//Execute response text on success
req.onload = function() {
scriptFromString(this.responseText);
}
//Iterate on error
req.onerror = function() {
if(index+1<scripts.length) {
loadScript(scripts, name, index);
}
else {
throw new Error("All sources failed for '"+name+"'.");
}
}
req.send();
}
我怎样才能克服这个问题?为什么通过
src
加载脚本是可以的,但ajax请求会引发错误?与其尝试使用XHR查询获取js文件,不如通过创建一个新的
元素并将其src=
属性设置为您尝试加载的文件,通过DOM加载它?因为它是DOM,所以跨域加载是合法的
function loadScript(scripts, name, index) {
//Convert single string to array - not effective but easy
if (!(scripts instanceof Array)) scripts = [scripts];
//Use first script src if no name is defined
if (!name) name = scripts[0];
//Default index is 0
if (!index) index = 0;
//In case wrong index was provided
if (index >= scripts.length) throw "Array index out of bounds.";
//Create request
var include = document.createElement('script');
with(include) {
type = "text/javascript";
src = scripts[index];
onload = function () {
return;
};
onerror = function () {
if (++index < scripts.length) {
loadScript(scripts, name, index);
} else {
throw "All sources failed for '" + name + "'.";
}
};
}
document.head.appendChild(include);
}
函数加载脚本(脚本、名称、索引){
//将单个字符串转换为数组-无效但简单
如果(!(脚本数组实例))脚本=[scripts];
//如果未定义名称,请使用第一个脚本src
如果(!name)name=scripts[0];
//默认索引为0
如果(!index)index=0;
//以防提供错误的索引
如果(index>=scripts.length)抛出“数组索引超出范围”;
//创建请求
var include=document.createElement('script');
连同(包括){
type=“text/javascript”;
src=脚本[索引];
onload=函数(){
返回;
};
onerror=函数(){
如果(++索引
(不确定您对
name
参数/变量做了什么,所以我把它放在一边。您可能应该使用name
的任何逻辑)因为我猜有人在发明验尸官时考虑得不够。我认为脚本标记不存在onerror事件。onerror
在我的JSFIDLE中,当我故意错误命名脚本文件时触发。它的工作原理与
元素的onerror
事件相同。这是在Firefox中。您可能应该在其他浏览器中进行测试,但我非常确定它与所有现代浏览器都是兼容的。@TomášZato您有什么回应或反馈吗?我这边有很多事情要做。正如你所看到的,我已经是SO的活跃成员一段时间了,我最终利用了我得到的答案。但请给我时间,我真诚地道歉。你需要多长时间都行。
function loadScript(scripts, name, index) {
//Convert single string to array - not effective but easy
if (!(scripts instanceof Array)) scripts = [scripts];
//Use first script src if no name is defined
if (!name) name = scripts[0];
//Default index is 0
if (!index) index = 0;
//In case wrong index was provided
if (index >= scripts.length) throw "Array index out of bounds.";
//Create request
var include = document.createElement('script');
with(include) {
type = "text/javascript";
src = scripts[index];
onload = function () {
return;
};
onerror = function () {
if (++index < scripts.length) {
loadScript(scripts, name, index);
} else {
throw "All sources failed for '" + name + "'.";
}
};
}
document.head.appendChild(include);
}