Horje
HTML onerror Event Attribute

Definition and Usage

The onerror attribute fires when an error occurs while loading an external file (e.g. a document or an image).


Browser Support

Syntax

<element onerror="script">

Attribute Values

Value Description
script The script to be run on onerror

Technical Details

Supported HTML tags: <img>, <input type="image">, <object>, <link>, and <script>

Example of HTML onerror Event Attribute

Here executes a JavaScript if an error occurs when loading an image.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<img src="https://horje.com/avatar.png" onerror="myFunction()">

<p>A function is triggered if an error occurs when loading the image. The function shows an alert box with a text. In this example we refer to an image that does not exist, therefore the onerror event occurs.</p>

<script>
function myFunction() {
  alert("The image could not be loaded.");
}
</script>

</body>
</html>

Output should be:

How to add a JavaScript if an error occurs when loading an image

A function is triggered if an error occurs when loading the image. The function shows an alert box with a text. In this example we refer to an image that does not exist, therefore the onerror event occurs.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<img src="https://horje.com/avatar.png" onerror="myFunction()">

<p>A function is triggered if an error occurs when loading the image. The function shows an alert box with a text. In this example we refer to an image that does not exist, therefore the onerror event occurs.</p>

<script>
function myFunction() {
  alert("The image could not be loaded.");
}
</script>

</body>
</html>

Output should be:

How to add a JavaScript if an error occurs when loading an image




html event attributes

Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Window Event Attributes
Uploaded by:
Admin


Read Article
https://develop.horje.com/learn/1434/reference