EN
HTML - onerror event attribute
0 points
In this article, we would like to show you how to use onerror event attribute in HTML.
Quick solution:
xxxxxxxxxx
1
<img src="https://wrong-url.com/image.png" onerror="myFunction()" />
In this example, we create handleError()
function and assign it to the HTML onerror
event attribute so it will be executed when the image loading error occurs.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<script>
5
6
function handleError(image) {
7
console.error('Image loading error (' + image.src + ').');
8
}
9
10
</script>
11
<img src="https://wrong-url.com/image.png"
12
alt="Image loading error."
13
onerror="handleError(this)" />
14
</body>
15
</html>