当尝试追加子脚本元素由于未授权而失败时抛出什么错误?

What error is thrown when trying to appendChild script element fails due to unauthorized

本文关键字:失败 错误 什么 授权 于未 追加 脚本 元素      更新时间:2023-09-26

我想捕获这段JavaScript代码的一个特定错误:

var script = $wnd.document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');

脚本所在的url需要用户登录,因此返回HTTP 401 Unauthorized错误。

我所理解的错误(在try/catch中)可以接受的值似乎都不太匹配。

EvalError: eval()函数发生错误。
RangeError: number Out of range value .
ReferenceError:发生非法引用。
SyntaxError:eval()函数内部的代码中发生语法错误。事件。
TypeError:预期变量类型发生错误。
URIError:编码或解码URI时发生错误(即:调用encodeURI()时)。

是否有任何方法可以捕获这个401错误,或者至少是由于无法加载脚本而抛出的IO错误。

谢谢

script.addEventListener('error', function(){
    // Didn't load
}, true);