javascript - Firefox "onerror" would not fire even when image is not valid - Stack Overflow

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

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.

.jpg

<img onerror="this.src='/images/pic_not_found.png'" src=".jpg">

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

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./profile_images/522455109/calvin-and-hobbessm_normal.jpg

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

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

Share Improve this question asked Oct 14, 2010 at 23:32 meowmeow 28.2k36 gold badges121 silver badges178 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It is not a broken link.

The twimg. actually returns an image with the name of the url you requested.

just click 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.
It does some basic feature detection.

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

http://jsfiddle/VVcQj/1

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204964a4616549.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信