Firefox“onerror”即使图像无效也不会启动 [英] Firefox "onerror" would not fire even when image is not valid

查看:203
本文介绍了Firefox“onerror”即使图像无效也不会启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在image标签上有一个onerror处理程序来处理没有找到远程图像时的切换。

问题是,对于某些破碎的远程图像,它不起作用。





它同时使用 onload onerror ,但需要在javascript中定义一个函数来处理这种情况。


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">

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

解决方案

It is not a broken link.

The twimg.com 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.net/VVcQj/1

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

这篇关于Firefox“onerror”即使图像无效也不会启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆