onerror
特性为
GlobalEventHandlers
混合
EventHandler
处理
error
事件。
Error events are fired at various targets for different kinds of errors:
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)).
<img>
or
<script>
)
fails to load
,
error
event using interface
事件
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
window.addEventListener
configured with
useCapture
set to True.
Installing a global
error
event handler is useful for automated collection of error reports.
For historical reasons, different arguments are passed to
window.onerror
and
element.onerror
handlers (as well as on error-type
window.addEventListener
handlers).
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)
error
:
Error Object
(object)
When the function returns
true
, this prevents the firing of the default event handler.
window.addEventListener('error', function(event) { ... })
event
类型
ErrorEvent
contains all the information about the event and the error.
element.onerror = function(event) { ... }
element.onerror
accepts a function with a single argument of type
事件
.
A good example for this is when you are using an image tag, and need to specify a backup image in case the one you need is not available on the server for any reason.
<img src="imagenotfound.gif" onerror="this.onerror=null;this.src='imagefound.gif';" />
The reason we have the
this.onerror=null
in the function is that the browser will be stuck in an endless loop if the onerror image itself generates an error.
When an error occurs in a script, loaded from a
different origin
, the details of the error are not reported to prevent leaking information (see
bug 363897
). Instead the error reported is simply
"Script error."
This behavior can be overriden in some browsers using the
attribute on
crossorigin
<script>
and having the server send the appropriate
CORS
HTTP response headers. A workaround is to isolate "Script error." and handle it knowing that the error detail is only viewable in the browser console and not accessible via 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;
};
When using the inline HTML markup (
<body onerror="alert('an error occurred')">
), the HTML specification requires arguments passed to
onerror
to be named
event
,
source
,
lineno
,
colno
,
error
. In browsers that have not implemented this requirement, they can still be obtained via
arguments[0]
透过
arguments[2]
.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'onerror' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
onerror
|
Chrome 10 | Edge 12 | Firefox 1 | IE 9 | Opera 11.6 | Safari 6 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 |
完整支持
GlobalEventHandlers
onabort
onanimationcancel
onanimationend
onanimationiteration
onauxclick
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncuechange
ondblclick
ondurationchange
onended
onerror
onfocus
onformdata
ongotpointercapture
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onload
onloadeddata
onloadedmetadata
onloadend
onloadstart
onlostpointercapture
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onpause
onplay
onplaying
onpointercancel
onpointerdown
onpointerenter
onpointerleave
onpointermove
onpointerout
onpointerover
onpointerup
onreset
onresize
onscroll
onselect
onselectionchange
onselectstart
onsubmit
ontouchcancel
ontouchstart
ontransitioncancel
ontransitionend
onwheel
BeforeUnloadEvent
DOMStringMap
ErrorEvent
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers