/    Sign up×
Community /Pin to ProfileBookmark

Preloading Images in IE9

I’m finding IE9 handles the preloading of images differently to IE8 and FF4 and the latest versions of other browsers.

In IE9 the preloading still works ok unless I want to use the width or height properties of the created Image object before the src of the new Image has been assigned to an <img>

For example:

This code in the <head>

[CODE]
<script type=”text/javascript”>

var picData = [‘mypic1.jpg’,’mypic2.jpg’,’mypic3.jpg’,’mypic4.jpg’];
picO = new Array();

for(i=0; i < picData.length; i++){
picO[i] = new Image();
picO[i].src = picData[i];
}

window.onload=function(){
alert(picO[0].width+’ ‘+picO[0].height);
}
</script>[/CODE]

outputs ‘0 0’ in IE9 and ‘150 250’ in FF4 and the latest versions of the major browsers. 150 and 250 are the actual width and height of mypic1.jpg

Earlier versions of IE seem to work as FF4 does.

It appears that the window.onload event in IE9 does not take into account if all the images have completed downloading via the above FOR loop whereas FF4 and the others do.

The workaround I am using is to preload the images in hidden <img>’s at the top of the <body> and then loading them into an Image object to get the jpg’s actual width and height. This is working in IE9, FF4 and the other majors.

Have I missed or misunderstood something in the the way IE9 works?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorApr 14.2011 — I can not confirm that. IE9 works OK, same as FF4, IE7, IE8...

IE9/Win7 (32) here.
Copy linkTweet thisAlerts:
@webdev1958authorApr 15.2011 — RESOLVED - elsewhere ?
×

Success!

Help @webdev1958 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...