views:

41

answers:

1

Hi folks, I have a onerror handler on the image tag to handle switching when the remote image is not found.

the problem is that for certain broken remote images, it 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"&gt;

Image below: 1) when remote image found, 2) remote image not found (onerror not triggered) , 3) remote image not found (onerror triggered)

alt text

+1  A: 

It is not a broken link ...

The twimg.com actually returns an image with the name of the url you requested.. so an image is returned ..

just clink your link to the image. It is not text what you are seeing .. it is an image..

Update

Here is some code that works in all browsers... does some basic feature detection ..

http://jsfiddle.net/VVcQj/1

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

Gaby
It is broken for me. are we looking at the same thing? http://a3.twimg.com/profile_images/522455109/calvin-and-hobbessm_normal.jpg
ming yeow
@ming, i see... firefox does something weird (*creates an image with the url*)..
Gaby
@ming, .. but the `onload` error works for me for this particular image.. http://jsfiddle.net/nenwQ/
Gaby
jsfiddle.net/nenwQ - works in chrome, not in firefox. Weird huh!
ming yeow
@ming, updated my answer with a solution..
Gaby
Hey Gaby, thanks a lot! This is amazingly smart! However, Chrome throws an error: "Uncaught SyntaxError: Unexpected token ILLEGAL" for the function when I define it. Any idea what is wrong?
ming yeow
@ming i can only reproduce the error you mention when i mismatch `'` around the strings inside the `onerror=..` or `onload=...`
Gaby
Thanks a lot gabe! it was an weird error with chrome - i moved the function downwards, and things worked well. Thanks a MILLION!
ming yeow
@ming, glad you got it sorted out :)
Gaby
Thanks to you. =) it is interesting how minute the differences in browsers can be
ming yeow
@ming, **interesting** is a kind word :) ...
Gaby