3

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.

Robby Bennett
  • 133
  • 1
  • 9

1 Answers1

3

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

Community
  • 1
  • 1
Lal
  • 14,726
  • 4
  • 45
  • 70