Computer >> Computer tutorials >  >> Programming >> HTML

Execute a script when an error occurs in HTML?


The onerror event triggers when an error occurs in HTML. You can try to run the following code to implement onerror event attribute −

Example

<!DOCTYPE html>
<html>
   <body>

      <img src = "new.png" onerror = "display()">
      <p>Alert would be visible, if the image isn't loaded correctly.</p>

      <script>
         function display() {
            alert("Image load unsuccessful.");
         }
      </script>
   </body>
   
</html>