I have an image in a HTML page:
If the image is not found o
The best way to solve your problem:
<img id="currentPhoto" src="SomeImage.jpg" onerror="this.onerror=null; this.src='Default.jpg'" alt="" width="100" height="120">
onerror
is a good thing for you :)
Just change the image file name and try yourself.
For the alternative, if the image doesn't exist - show nothing at all. (what I was looking for)
You can swap the function from Robby Shaw's answer in the "onerror" attribute to "this.remove()".
<img id="currentPhoto" src="SomeImage.jpg" alt='1' width="100" height="120">
<img id="currentPhoto" src="SomeImage.jpg" onerror="this.onerror=null; this.remove();" alt="2" width="100" height="120">
Ok but I like to use this way, so that whenever original image is not available you can load your default image that may be your favorite smiley or image saying Sorry ! Not Available, But in case if both the images are missing you can use text to display. where you can also you smiley then. have a look almost covers every case.
<img src="path_to_original_img/img.png" alt="Sorry! Image not available at this time"
onError="this.onerror=null;this.src='<?=base_url()?>assets1/img/default.jpg';">
You can show an alternative text by adding alt
:
<img src="my_img.png" alt="alternative text" border="0" />
its works for me that if you dont want to use alt attribute if image break then you can use this piece of code and set accordingly.
<h1>
<a>
<object data="~/img/Logo.jpg" type="image/png">
Your Custom Text Here
</object>
</a>
</h1>
simple way to handle this, just add an background image.