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 that occur when the media loading process is interfered with are:

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

Bubbling: Not supported
Cancelable: Not supported
Event type: If generated from the user interface,UiEvent. Otherwise Event.
Supported HTML tags: <img>, <input type="image">, <object>, <link> and <script>
DOM Version: Level 2 Events

Browser Support

Events Chrome IE Firefox Safari Opera
onerror Support Support Support Support Support