onerror event

Definition and usage

If an error occurs while loading an external file (such as a document or image), the onerror event will be triggered.

Tip:When used for audio/video media, related events occur when the media loading process is disturbed by something:

Example

If an error occurs while loading the image, execute JavaScript:

<img src="image.gif" onerror="myFunction()">

Try it yourself

Syntax

In HTML:

<element onerror="myScript">

Try it yourself

In JavaScript:

object.onerror = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("error", myScript);

Try it yourself

Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubble: Not supported
Cancelable: Not supported
Event type: If generated from the user interfaceUiEventOtherwise Event.
Supported HTML tags: , , , 以及