Js image onload event usage problem

  • 2020-03-30 03:31:29
  • OfStack

About the Image object onload event.

What you want to get is the width and height of the image. So the onload method. The other is the remote image obtained.

But IE never USES the onload method the second time it displays the image. Code:


var img = new Image();
img.src = "loading.gif";
img.onload = function(){
var iheight = img.height;
var iwidth = img.width;
};

Later, after I showed it to xiaoniu, I realized that img. SRC should be placed after the onload method.

Later,, Google, only to find that the original problem we have more summary. I'm a layman.

Because IE did not trigger the onload method when loading buffer data, the code in this event was ignored. FF fires the onload method in passing while loading the buffer data.

In fact, there is a point, is the onload when the image is also adaptive adjustment, I found that the need to onload also set the element img. SRC... Or it just remembers the first picture...

There's still a little bit of understanding of this cache. To continue.


Related articles: