Monday, August 02, 2010

IMG onError Attribute

There always is a lot of talk going on about jQuery and similar such frameworks. There have been loads of exciting things that can be done with them as well. But I still sometimes remain unsure about some of the things that are done with jQuery which can instead be done by simple Javascript? Maybe this isn't supported by all browsers or something but am not really sure.

There's a jQuery plugin to display an alternative image when the current image we are looking up results in an error.

Am not so sure if what am going to suggest is the right way of doing it, but probably one of the ways of doing it I assume.

For the IMG Tag in HTML, you do have an onError attribute where you can specify that if the image didnt load, then load another existing image. 

So for example, if your looking at:

<img class="thumbnail" width="75" height="50" src="sourceImage" alt="Alt Text to display"  onerror="this.src='defaultExistingImage'" />
 
And in this case sourceImage is referring to an image that doesnt exist, the browser invokes the onError event using which you could specify the source for the image to a default one.

Sounds simple, and its been tested on a wide variety of browsers when I looked up the net. So the newer ones including the standard IE6/7,Firefox and Chrome do support it.

Now am not sure why use jQuery when you can do something as simple as this?

Labels: , , , ,

0 Comments:

Post a Comment

<< Home