为什么所有其他浏览器(IE7、8、9,FF,Opera和Safari)都反复调用img元素时,Chrome只为img元素调用onerror事件一次?
有没有办法强迫它再次重复onerror调用(在Chrome中)?
HTML:
<div id="thisWorks">
this works in Chrome. onerror event is called once.
<img src="http://www.asdfjklasdfasdf.com/bogus1.png"
onerror="fixit(this);"
rsrc="http://eatfrenzy.com/images/success-tick.png" />
</div>
<div id="thisDoesNotWork">
this does not work in Chrome. onerror event is not called twice.
<img src="http://www.asdfjklasdfasdf.com/bogus1.png"
onerror="fixit(this);"
rsrc="http://www.asdfjklasdfasdf.com/bogus2.png|http://eatfrenzy.com/images/success-tick.png" />
</div>
JAVASCRIPT:
function fixit(img)
{
var arrPhotos = img.getAttribute('rsrc').split('|');
// change the img src to the next available
img.setAttribute('src', arrPhotos.shift());
// now put back the image list (with one less) into the rsrc attr
img.setAttribute('rsrc', arrPhotos.join('|'));
return true;
}
编辑:根据@Sunil D.的评论,www.asdfjklasdfasdf.com
在最初的小提琴示例中,由于无效域名,Chrome无法发布新的查询,我继续进行操作,并更改了域名以匹配成功映像的域名,但文件名不同因此它仍然是404。这将证明它不是无效的域名,导致Chrome在第二次尝试时无法使用。
编辑:更新了小提琴并删除了对jquery的使用,以简单地处理并排除该情况。
好,知道了。在分配给onerror事件的函数内,将src
属性设置为,null
然后再将其更改为新值。
img.setAttribute('src', null);
工作提琴
这会导致Chrome重新设置它,如果后续值src
返回错误,将迫使它重复调用onerror 。
注意:空字符串不起作用,必须为null
。
注意2:此修复程序使用纯javascript(但不适用于jquery.attr
方法)工作。在发布此解决方案后,我尝试使用jquery.attr
方法将其设置为,$img.attr('src', null);
但这种方式不起作用,当我将其更改为javascript时,它可以工作。我也尝试使用jquery.prop
方法来尝试它,就像这样$img.prop('src', null);
,它第一次起作用,但随后的几次刷新失败。只有纯Javascript似乎是surefire解决方案。
更新:
好的,事实证明,虽然上述更改修复了Chrome并导致其像所有其他浏览器(FF,Safari,Opera,IE7-9)一样反复调用onerror,但它导致IE10的onerror事件出现问题,因此忽略了任何在上一行src
将其设置为后分配给的有效图像=null
。(...叹)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句