From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/globaleventhandlers/onerror/index.html | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 files/zh-cn/web/api/globaleventhandlers/onerror/index.html (limited to 'files/zh-cn/web/api/globaleventhandlers/onerror') diff --git a/files/zh-cn/web/api/globaleventhandlers/onerror/index.html b/files/zh-cn/web/api/globaleventhandlers/onerror/index.html new file mode 100644 index 0000000000..2f06929c48 --- /dev/null +++ b/files/zh-cn/web/api/globaleventhandlers/onerror/index.html @@ -0,0 +1,114 @@ +--- +title: GlobalEventHandlers.onerror +slug: Web/API/GlobalEventHandlers/onerror +tags: + - API + - Property + - Reference +translation_of: Web/API/GlobalEventHandlers/onerror +--- +
{{ApiRef("HTML DOM")}}
+ +
混合事件 {{domxref("GlobalEventHandlers")}} 的 onerror 属性是用于处理 {{event("error")}} 的事件
+ +
+ +

Error事件的事件处理程序,在各种目标对象的不同类型错误被触发:

+ + + +

加载一个全局的error事件处理函数可用于自动收集错误报告。

+ +

语法

+ +

由于历史原因,window.onerrorelement.onerror接受不同的参数。

+ +

window.onerror

+ +
window.onerror = function(message, source, lineno, colno, error) { ... }
+
+ +

函数参数:

+ + + +

若该函数返回true,则阻止执行默认事件处理函数。

+ +

window.addEventListener('error')

+ +
window.addEventListener('error', function(event) { ... })
+
+ +

{{domxref("ErrorEvent")}} 类型的event包含有关事件和错误的所有信息。

+ +

element.onerror

+ +
element.onerror = function(event) { ... }
+
+ +

element.onerror使用单一{{domxref("Event")}}参数的函数作为其处理函数。

+ +

注意事项

+ +

当加载自不同域的脚本中发生语法错误时,为避免信息泄露(参见{{bug("363897")}}),语法错误的细节将不会报告,而代之简单的"Script error."。在某些浏览器中,通过在{{HTMLElement("script")}}使用{{htmlattrxref("crossorigin","script")}}属性并要求服务器发送适当的 CORS HTTP 响应头,该行为可被覆盖。一个变通方案是单独处理"Script error.",告知错误详情仅能通过浏览器控制台查看,无法通过JavaScript访问。

+ +
window.onerror = function (msg, url, lineNo, columnNo, error) {
+    var string = msg.toLowerCase();
+    var substring = "script error";
+    if (string.indexOf(substring) > -1){
+        alert('Script Error: See Browser Console for Detail');
+    } else {
+        var message = [
+            'Message: ' + msg,
+            'URL: ' + url,
+            'Line: ' + lineNo,
+            'Column: ' + columnNo,
+            'Error object: ' + JSON.stringify(error)
+        ].join(' - ');
+
+        alert(message);
+    }
+
+    return false;
+};
+ +

当使用行内HTML标签(<body onerror="alert('an error occurred')">)时,HTML规范要求传递给onerror的参数命名为eventsourcelinenocolnoerror。针对不满足此要求的浏览器,传递的参数仍可使用arguments[0]arguments[2]来获取。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{SpecName('HTML WHATWG','webappapis.html#handler-onerror','onerror')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ +

在Firefox 14之前,当{{HTMLElement("script")}}加载失败时,window.onerror被传入"Error loading script"信息。该bug已在{{bug("737087")}}修复,取而代之,在这种情况下,scriptElement.onerror将被触发。

+ +

自Firefox 31始加入最后两个参数(colno and error),意味着通过提供的Error对象,你可以从window.onerror访问脚本错误的stack trace({{bug("355430")}}。)

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf