An invalid fix for the image onload event in IE

  • 2020-03-30 02:44:10
  • OfStack

Story mode is a photo of loading only currently browsing and the two pictures below it, loading photos to load and render the comments section, pictures not loaded with a picture of a pixel before placeholder, and a loading class will show a loading background, in the visual area replacing real images, image loaded successfully removed after loading classes.

The problem was at the end. During the test, it was found that the loading class under IE could not be deleted. At that time, the code was as follows:


img.src = _src;
img.src = _src;
img.onload = function(){
   _con.delClass('loading');
}

After searching the Internet, onload and the statement defining SRC should be changed in order. IE takes pictures from the cache. Onload does not trigger at all

img.onload = function(){
   _con.delClass('loading');
};
img.src = _src;

It was immediately normal


Conclusion: you should put the onload in front of SRC and tell the browser what to do with the image after it has loaded. So, it's not that Internet explorer doesn't fire the onload event, it's that the buffer is loaded so fast that it's already loaded without telling it what to do. On the other hand, firefox is significantly smarter. When the onload event is added, the firefox browser checks to see if the image is already in the buffer.


Related articles: