EN
HTML - display text message when image is not loaded correctly
3 points
In this article, we would like to show you how to display text message when the image is not loaded correctly in HTML.
Quick solution:
xxxxxxxxxx
1
<img src="https://wrong-url.com/image.png" alt="Error message." />
There are many reasons that may cause image loading errors: incorrect URL, network connection problems, incorrect server configuration, incorrect image file.
In this example, we display text message when the image isn't loaded correctly using alt
attribute. The message about loading error is displayed in the console too using onerror
event.
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" alt="Image loading error." onerror="handleError(this)" />
12
</body>
13
</html>