![]() |
Definition and UsageThe onerror attribute fires when an error occurs while loading an external file (e.g. a document or an image). Browser SupportSyntax
Attribute Values
Technical Details
|
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>
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>
html event attributes |
HTML onafterprint Event Attribute | Window Event Attributes |
HTML onbeforeprint Event Attribute | Window Event Attributes |
HTML onbeforeunload Event Attribute | Window Event Attributes |
HTML onerror Event Attribute | Window Event Attributes |
HTML onhashchange Event Attribute | Window Event Attributes |
HTML onload Event Attribute | Window Event Attributes |
HTML onoffline Event Attribute | Window Event Attributes |
HTML ononline Event Attribute | Window Event Attributes |
HTML onpageshow Event Attribute | Window Event Attributes |
HTML onresize Event Attribute | Window Event Attributes |
HTML onunload Event Attribute | Window Event Attributes |
How to add a JavaScript if an error occurs when loading an image | Window Event Attributes |
Type: | Develop |
Category: | Web Tutorial |
Sub Category: | Window Event Attributes |
Uploaded by: | Admin |
Reffered: https://www.w3schools.com/tags/ev_onerror.asp