Set alternate image html
In some situations, the image path set by user may not return any image but null value .(When image is removed from the provided path or may be due to fault in path provided in the tag.)
For these situations we can set an alternative image path if the invalid image path.
1) In line HTML
2) Call a Function when error occur
In Line HTML
In line HTML is used to give alternative image path directly.
<img src=" Images/Employees/smile.jpg" onerror="this.onerror=null;img/noimg.jpg;" />
Using Function in on error
Suppose you want to set the same alternate image for a number of img controls and you are passing the alternate image directly in ‘onerror’ attr for each of these controls .If a situation arise wherein you have to change the alternate image path ,then you will have to change the image path in each of these controls which may take a lot of time and effort may lead to some errors. So in such situations ,it’s better to use a function in oneerror attr (as shown in the eg : below ) rather than passing image path directly wherein you just have to change the image path in the function once and thus saves a lot of time and effort.
<script>
function callfun(obj)
{
var noimg = "Images/Employees/noimagepath.jpg";
obj.src=noimg;
}
</script>
<img src=" Images/Employees/erro.jpg" onerror="this.onerror=null;callfun(this);" />
0 comments:
Post a Comment