Why is Chrome's onerror event for the img element only fired once?

孤人 提交于 2019-11-29 06:52:34

I've tried the ways metioned above, setAttribute('src', null) has a side effect, ie, add another request.

Finally, I use

    setTimeout(function(){ imgElement.src = 'http://xxxx'; }, 0)

, and this works!

See the jsfiddle for example.

Okay got it. Inside the function you assign to the onerror event, set the src attribute to null before changing it to it's new value.

img.setAttribute('src', null);

working fiddle

This somehow causes Chrome to reset it and will force it to repeatedly call onerror if subsequent values for src return an error.

Note: an empty string won't work and needs to be null.
Note2: this fix works using pure javascript (but not with the jquery .attr method). After I posted this solution I tried it with the jquery .attr method setting it to $img.attr('src', null); but it didn't work that way and when I changed it to javascript, it worked. I also tried it using the jquery .prop method instead like so $img.prop('src', null); which worked the first time and failed on a few subsequent refreshes. Only the pure javascript seems to be a surefire solution.

UPDATE:
Okay, turns out that while the above change fixes Chrome and causes it to repeatedly call onerror like all other other browsers (FF, Safari, Opera, IE7-9), it causes problems with the onerror event for IE10 and thus ignores any valid images assigned to src after setting it to =null on the previous line. (...sigh).

This is the correct behavior. You don't want to download the same image twice -- that's a waste of bandwidth.

What Chrome does it create an in-memory object and then will apply it to all images with the respective src.

If you need it downloaded twice, then create those objects through javascript and assign .onload=function() { .. } to each.

I think @Mikhail is on the right track, except for a slightly different reason. In the second example, you attempt to download 2 images from the same non-existent domain www.asdfjklasdfasdf.com.

When attempting to download bogus1.png, Chrome fails to resolve the domain to an IP address.

When attempting to download bogus2.png (from the same non-existent domain), Chrome doesn't do anything at all... because it knows the domain lookup has failed. Whether it's correct behavior for chrome not to dispatch another onerror event might be open to debate :) I sort of expect that it should.

To prove this, simply add 1 character to the domain name for bogus2.png and it will work as you expected.

Edit

One way you could force it to attempt the subsequent downloads is to change the src of the <img> to the empty string. It's kind of hacky, but it works. You could set your rsrc attribute to something like this:

rsrc="''|http://www.asdfjklasdfasdf.com/bogus2.png|''|http://eatfrenzy.com/images/success-tick.png"

This way, when the error occurs, you set the source to ''. This seems to generate an error too, which then triggers it to try the next source...

Dany Gauthier

As answered by Etdashou on this question : https://stackoverflow.com/a/9891041/1090274, setting this.onerror=null; worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!