Solve the problem of image onload event invalidation in IE

Source: Internet
Author: User

In the past two days, a simple Image Viewer is created, where "Previous Page" and "next page" are used, new image () is used for Image loading, and the src attribute of image is set, everything works normally in Google Chrome/Opera/FireFox. The Code is as follows:

Copy to ClipboardReference: [www.bkjia.com] var I = new Image ();
I. src = _ this. settings. imgs [_ this. currIndex];
// Baidu. dom. show (baidu. dom. query (selectPre + '# loading') [0]);
I. onload = function (){
Image. src = I. src;
Baidu. dom. hide (baidu. dom. query (selectPre + '# loading') [0]);
Baidu. dom. query ('#' + _ this. settings. formId + '. title ') [0]. innerHTML = I. src. replace (/(. * \/)/ig, '') + '-' + _ this. settings. title;
}

Test results:

However, in IE8/IE7, the problem occurs: When all the images are turned over or refreshed at a time, the images will not be displayed after clicking, the following figure shows the refresh result (the red image is the loading icon ):

Is the onload of the Image object invalid? When I try to bring up a dialog box in the onload event, IE is invalid, which confirms that the onload event has not occurred. I searched the following materials online:

When IE re-displays the image, it does not send the image from the server, but obtains the last cached image from the cache, so that the loading speed from the cache is very fast, therefore, the onload event has been triggered before onload is run, which causes the code in the onload event to fail to be executed.

In this case, we can write the onload event function before the statement that assigns a value to src. This solves the problem. The Code is as follows:

Copy to ClipboardReference: [www.bkjia.com] var I = new Image ();
// Baidu. dom. show (baidu. dom. query (selectPre + '# loading') [0]);
I. onload = function (){
Image. src = I. src;
// Baidu. dom. hide (baidu. dom. query (selectPre + '# loading') [0]);
// Baidu. dom. query ('#' + _ this. settings. formId + '. title ') [0]. innerHTML = I. src. replace (/(. * \/)/ig, '') + '-' + _ this. settings. title;
}
I. src = _ this. settings. imgs [_ this. currIndex];

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.