2012-07-27

Set a placeholder image when the image is not available

When you are using img tags, sometimes you need to set a default image for the img tag if the source image is not found or corrupted. There is an event for the img tag called onerror. By using that event, we can easily set a default placeholder image for the img tag, if the image failed to load. Following is an example for the onerror method.

<script>
    function test(e)
    {
       e.src="images/placeholder.jpg";
    }
<script>

<img onerror="test(this);" src="......." />

Now the test() method will get called whenever there is a problem loading the image to the src and will set the placeholder.jpg as the image src.

2 comments:

kiran said...

Thank u very very much it helped me a lot..thanks.......

kiran said...

Thank u very very much ...
it helped me a lot......
one again thanks...