I have an image in a HTML page:
If the image is not found o
Here Check below code snippet which, In this, I miss-spelled the image name. So, just because of that it is showing the alternative image as not found image ( 404.svg ).
<img id="currentPhoto" src="https://www.ccrharrow.org/Images/content/953/741209.pg" onerror="this.src='https://www.unesale.com/ProductImages/Large/notfound.png'" alt="">
This one worked for me. using the srcset. I have just learnt about it so i dont know if browsers support it but it has worked for me. Try it and later give me your feed back.
<img src="smiley.gif" srcset="alternatve.gif" width="32" height="32" />
The usual way to handle this scenario is by setting the alt
tag to something meaningful.
If you want a default image instead, then I suggest using a server-side technology to serve up your images, called using a similar format to:
<img src="ImageHandler.aspx?Img=Blue.jpg" alt="I am a picture" />
In the ImageHandler.aspx
code, catch any file-not-found errors and serve up your default.jpg
instead.
try PHP
if (file_exists('url/img/' . $Image)) {
$show_img_URL = "Image.jpg";
} else {
$show_img_URL = "Image_not_found.jpg";
}
Try using border=0
in the img
tag to make the ugly square go away.
<img src="someimage.png" border="0" alt="some alternate text" />
If you want an alternative image instead of a text, you can as well use php:
$file="smiley.gif";
$alt_file="alt.gif";
if(file_exist($file)){
echo "<img src='".$file."' border="0" />";
}else if($alt_file){
// the alternative file too might not exist not exist
echo "<img src='".$alt_file."' border="0" />";
}else{
echo "smily face";
}