The problem of the OnLoad event invalidation and the solution _javascript technique of the picture in IE

Source: Internet
Author: User

In the Web development in the acquisition of the picture is very normal things, the picture before loading is not get the picture of the width of the high, after the load is completed before you can get the width of the picture itself, for example:

Copy Code code as follows:

var img = new Image ();
IMG.SRC = "Loading.gif";
Img.onload = function () {
alert (img.width);
};

OK? This code looks no problem, but in IE there will be a bug, IE is the first time to open the time no problem, the second use of this method on the tragedy, IE did not respond, even if the Refresh page is the same. Because IE will cache the picture, the 2nd time to load the picture, not from the server upload, but from the buffer in Riga.
Write the OnLoad method first, then specify the URL of this picture, this is normal. Therefore, not IE did not trigger the OnLoad event, but because the speed of loading the buffer too fast, so that did not run to the Img.onload, the onload event has been triggered. That's OK.

Copy Code code as follows:

var img = new Image ();
Img.onload = function () {
alert (img.width);
};
IMG.SRC = "Loading.gif";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.