Firefox "onerror" does not fire even if the image is invalid

I have an onerror handler on an image tag to handle switching when a remote image is not found.

the problem is that for some broken deleted images this does not work.

http://a3.twimg.com/profile_images/522455109/calvin-and-hobbessm_normal.jpg

<img onerror="this.src='/images/pic_not_found.png'" src="http://a3.twimg.com/profile_images/522455109/calvin-and-hobbessm_normal.jpg"> 

Image below: 1) when a remote image is detected, 2) the remote image was not found (onerror does not start), 3) the remote image was not found (onerror triggered)

alt text

+4
source share
1 answer

This is not a broken link.

twimg.com actually returns an image with the name of the URL you are requesting.

just click on the link. This is not the text you see, this is an image.

Update

Here is the code that works in all browsers.
It performs some basic detection functions.

 function handle( elem, img, state ) { if ((typeof(elem.onerror) === 'function' && state === 'fail') || (elem.width === 0) ) { elem.src = img; } } 

http://jsfiddle.net/VVcQj/1

It uses both onload and onerror , but a function defined in javascript is required to handle the situation.

+5
source

All Articles