HTML onerror Event Attribute

Description

The event attribute onerror allows you to set the script to trigger when an error occurs while loading an external file.

Syntax

<element onerror="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html> 
  <html> 
    <head>
      <title>onerror event</title> 
    </head> 
    <body> 
    <img src="error.jpg" onerror="testFunction(this)"> 
<script> 
function testFunction(source) { 
  source.onerror = null;
  source.src = "sunset.jpg"; 
} 
</script> 
  </body> 
</html>

Supported Tags

<audio>, <body>, <embed>, <img>, <object>, <script>, <style> and <video>

Browser Support

Desktop
Explorer Edge Chrome Firefox Opera Safari
YesXYesYesXX
Tablets / Mobile
Android Chrome Firefox Opera Safari Samsung
YesYesYesXXX