It is normal for web development to obtain the width and height of the image. The image cannot obtain the width and height of the image before loading. Only after loading can the image be completed can the width and height of the image itself be obtained. For example:
The code copy is as follows:
var img = new Image();
img.src = "loading.gif";
img.onload = function(){
alert ( img.width );
};
OK? This code seems to be fine, but there will be a bug in ie, that is, there is no problem when ie is opened for the first time, and it will be a tragedy when using this method the second time. ie has no response, and it will be the same even if the page is refreshed. Because IE will cache images, the second loaded image is not uploaded from the server, but is loaded from the buffer.
First write the onload method, and then specify the URL of this image, which is normal. So, it is not that IE did not trigger the onload event, but because the buffer is loaded so fast that the onload event has already been fired when img.onload is not running. That's OK.
The code copy is as follows:
var img = new Image();
img.onload = function(){
alert ( img.width );
};
img.src = "loading.gif";