Home > Web Front-end > JS Tutorial > Problems and solutions to the invalid onload event of images in IE_javascript skills

Problems and solutions to the invalid onload event of images in IE_javascript skills

WBOY
Release: 2016-05-16 16:45:38
Original
1393 people have browsed it

In web development, it is normal to obtain the width and height of the image. The width and height of the image cannot be obtained before the image is loaded. The width and height of the image itself can be obtained only after the loading is completed, for example:

Copy code The code is as follows:

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

OK? There is nothing wrong with this code, but there is a bug in IE. It is fine when I open it for the first time, but it is a tragedy the second time I use this method. IE does not respond, even if I refresh it. The same goes for pages. Because IE caches images, the image loaded for the second time is not uploaded from the server, but loaded from the buffer.
Write the onload method first, and then specify the URL of this image, and it will be normal. Therefore, it is not that IE does not trigger the onload event, but because the buffer is loaded so fast that the onload event has already been triggered before img.onload is run. That's OK.

Copy code The code is as follows:

var img = new Image();
img .onload = function(){
alert ( img.width );
};
img.src = "loading.gif";
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template