Solution to invalid image onload event in IE browser

Source: Internet
Author: User

The implementation of the Story mode is to load only the currently viewed photo and the two photos below it, and load and render the comment area only when loading the photo, before an image is loaded, a one-pixel image will be taken into place, and a loading class will be used to display a loading background image to determine whether the image will be replaced with a real image in the visible area, after the image is loaded successfully, the loading class is deleted.

The problem lies at the end. During the test, we found that the loading class under IE could not be deleted. The code at that time was as follows:
Copy codeThe Code is as follows:
Img. src = _ src;
Img. src = _ src;
Img. onload = function (){
_ Con. delClass ('loading ');
}
I found it on the Internet. The onload and src-defined statements should change the order. IE extracts images from the cache. onload is not triggered at all. opera also has this problem. The correct code is modified as follows:
Copy codeThe Code is as follows:
Img. onload = function (){
_ Con. delClass ('loading ');
};
Img. src = _ src;
It's normal now


Conclusion: The onload should be written before src, first tell the browser how to deal with the image loading, and then let it load the image. Therefore, it is not that the IE browser will not trigger the onload event, but because the loading speed of the buffer is too fast. When it does not tell it what to do after loading, it is already loaded. On the other hand, firefox is obviously more intelligent. After the onload event is added, firefox will check whether the buffer zone already has this image. If yes, this event will be triggered directly!

Related Article

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.