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

Example:

<!DOCTYPE html> 
<html> 
  <head> 
       <title>onerror event attribute</title> 
  <style> 
    body { 
        text-align:center; 
        } 
     h1 { 
    color:green; 
        } 
  </style> 
     </head> 
  <body> 
    <img src="image.gif" onerror="myFunction()"> 
    <h1>Freshersnow</h1> 
    <h2>onerror event attribute</h2> 
    <script> 
    function myFunction() { 
    alert("The image could not be loaded."); 
    } 
    </script> 
  </body> 
</html>

Output:

HTML onerror attribute