YouTip LogoYouTip

Event Onerror

/* Styles from the original page would go here, kept as-is */ body { font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 0; color: #333; } .container { max-width: 1200px; margin: 0 auto; padding: 0 20px; } /* Add other necessary styles */

onerror Event

Example

Execute JavaScript if an error occurs when loading an image:

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

Definition and Usage

The onerror event is triggered when an error occurs while loading an external file (document or image).


Browser Support

Event
onerror Yes Yes Yes Yes Yes

Syntax

In HTML:

<element onerror="myScript">
Try it

In JavaScript:

object.onerror = function(){myScript};
Try it

In JavaScript, using the addEventListener() method:

object.addEventListener("error", myScript);
Try it

Note: Internet Explorer 8 and earlier IE versions do not support the addEventListener() method.


Technical Details

Bubbles: No
Cancelable: No
Event type: UIEvent if generated from the user interface, otherwise Event.
Supported HTML tags: <img>, <input type="image">, <object>, <script>, <style>