window.onerror事件用来自定义错误处理

Event reference:      https://developer.mozilla.org/en-US/docs/Web/Events

 http://w3c.github.io/html/webappapis.html#events

 

原文: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror

An event handler for the error event. Error events are fired at various targets for different kinds of errors:

  • When a JavaScript runtime error (including syntax errors and exceptions thrown within handlers) occurs, an error event using interface ErrorEvent is fired at window and window.onerror() is invoked (as well as handlers attached by window.addEventListener (not only capturing)).
  • When a resource (such as an <img> or <script>fails to load, an error event using interface Event is fired at the element that initiated the load, and the onerror() handler on the element is invoked. These error events do not bubble up to window, but (at least in Firefox) can be handled with a single capturing window.addEventListener.

Installing a global error event handler is useful for automated collection of error reports.

Syntax

For historical reasons, different arguments are passed to window.onerror and element.onerror handlers (as well as on error-type window.addEventListenerhandlers).

window.onerror

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

Function parameters:

  • message: error message (string). Available as event (sic!) in HTML onerror=""handler.
  • source: URL of the script where the error was raised (string)
  • lineno: Line number where error was raised (number)
  • colno: Column number for the line where the error occurred (number)
  • errorError Object (object)

When the function returns true, this prevents the firing of the default event handler.

window.addEventListener('error')

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

event of type ErrorEvent contains all the information about the event and the error.

 

 

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值