HTML onerror Attribute

HTML onerror Attribute: This attribute is defined as an error event occurs when an error occurs during the loading of a media. While the external file may contain a document file or image file. This attribute contains a single value script which works when onerror event attribute call.

HTML onerror Attribute

The HTML onerror attribute can be applied to <audio>,<embed>,<img>,<body>,<link>,<object>,<video>,<script> elements.

Syntax: <element onerror=”script”>

Browser Support

This attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Safari
  • Opera
  • Internet Explorer


<!DOCTYPE html> 
       <title>onerror event attribute</title> 
    body { 
     h1 { 
    <img src="image.gif" onerror="myFunction()"> 
    <h2>onerror event attribute</h2> 
    function myFunction() { 
    alert("The image could not be loaded."); 


HTML onerror attribute