Solution to IMG. onload failure in IE

Source: Internet
Author: User
<Doctype HTML public-wcdtd XHTML stricten httpwwwworgtrxhtmldtdxhtml-strictdtd> recently, we are working on web development. One of the requirements is to use JavaScript to obtain the size of the image to be loaded. Naturally, I thought of the IMG onload method. After the development is completed in Firefox, I went to IE for debugging and found that IMG onload events are not called in many cases.

My initial code is as follows:

VaR IMG = new image;
IMG. src = "test.gif ";
IMG. onload = function (){
Alert (IMG. width );
};

This code is okay, but why is onload not called by IE? Because ie caches the image and loads the image 2nd times, instead of being uploaded from the server, but from the buffer. Isn't the onload event not triggered for images loaded from the buffer? I tested the following code and succeeded ~

VaR IMG = new image;
IMG. onload = function (){
Alert (IMG. width );
};
IMG. src = "test.gif ";

I wrote onload to the front, and told the browser how to process the image first, and then specify the source of the image. This is normal. Therefore, it is not because IE does not trigger the onload event, but because the loading speed of the buffer is so fast that the onload event has been triggered when it is not running to IMG. onload. This reminds me of Ajax. When writing XMLHTTP, we specify the onstatechange callback function first and then send data. The truth is the same.

# JavaScript/ajax Columns

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.