/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Image preload full cache

I’m quite familiar with the standard technique for preloading images to be used in JavaScript effects, i.e.:

[CODE]var preloads = new Array();
preloads[0] = new Image();
preloads[0].src = ‘filename1.jpg’;
preloads[1] = new Image();
preloads[1].src = ‘filename2.jpg’;[/CODE]

However, this technique only initiates the HTTP requests for those images. The script execution continues as soon as the request is made. Is it at [I]all[/I] possible to ensure that script execution pauses until the images are fully downloaded? In other words, how do I make sure the complete images are present in the browser cache before the next script commands are run?

The practical application for this: I’m working on a script that dynamically builds its own interface, and it relies on appending the images into the actual document during the window.load script and capturing their offsetWidths. The images are immediately visible when the window.load script finishes, rather than waiting for any further user input to display them.

Both IE and Firefox are having trouble capturing the offsetWidths and displaying the images initially on the first page load, but every subsequent page load when the images are fully downloaded works fine.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliJul 02.2008 — Is it at [I]all[/I] possible to ensure that script execution pauses until the images are fully downloaded?[/quote]Use the images' onload events to count the images loaded; when the total is reached, execute the rest of your code as a function.
Copy linkTweet thisAlerts:
@tarsusauthorJul 02.2008 — I didn't realize elements like that had onload events. That's a great suggestion! I'll give it a try.
Copy linkTweet thisAlerts:
@tarsusauthorJul 02.2008 — Worked like a charm! That really is a great tip I'll be using a lot in the future. Thanks!
×

Success!

Help @tarsus 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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