Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup img if the first can't be found on HTML

I want to display img1.png. However, if there's an error finding the file, I want the backup missing.png to appear instead. Is there a way in HTML to have if statements or exceptions?

<try>?
    <img src="img1.png">
<except error 404>?
    <img src="missing.png">

I put a question mark on all the lines that I don't understand what to do.

like image 625
Robby Bennett Avatar asked Sep 15 '25 20:09

Robby Bennett


1 Answers

See the answer here

It says you can do it as below

<img src="img1.png" alt="Image not found" onError="this.onerror=null;this.src='missing.png';" />

where onerror is a Javascript function. Please refer the docs for more details about onerror

like image 151
Lal Avatar answered Sep 18 '25 13:09

Lal