/    Sign up×
Community /Pin to ProfileBookmark

Pre-load images.

What is the easiest method to ‘pre-load’ images using JS? ?

I have seen this term bandied about,
but I’m not sure how it differs from just loading images
or how might be done. 😮

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeApr 05.2012 — I've never done it, myself. But I did Google the following article, which seems pretty informative.

http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/
Copy linkTweet thisAlerts:
@Angry_Black_ManApr 05.2012 — interesting read. he didn't actually use AJAX to preload any type of image though, so why did he include that as a method?
Copy linkTweet thisAlerts:
@JMRKERauthorApr 05.2012 — I've never done it, myself. But I did Google the following article, which seems pretty informative.

http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/[/QUOTE]


Thanks. I'll give them a try to see if the script acts any faster.

?
Copy linkTweet thisAlerts:
@Logic_AliApr 06.2012 — Obviously images can be pre-loaded without JS using <img> with 0-width/height or css backgrounds, but in cases where the display of the pre-loaded images is script-dependent, then the preloading may as well be done by a script routine, ensuring that it's not done if script isn't supported.

I'm sure you've seen countless examples using the Image() object. Don't make the classic error of applying multiple filenames to the same instance of an Image object, using a loop.
Copy linkTweet thisAlerts:
@JMRKERauthorApr 06.2012 — Well, I tried the first JS only example in the link 'Wolfshade' provided,

but now I have more questions that before...
<i>
</i>&lt;div class="hidden"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--//--&gt;&lt;![CDATA[//&gt;&lt;!--
var images = new Array()
function preload() {
for (i = 0; i &lt; preload.arguments.length; i++) {
images[i] = new Image()
images[i].src = preload.arguments[i]
}
}
preload(
"http://domain.tld/gallery/image-001.jpg",
"http://domain.tld/gallery/image-002.jpg",
"http://domain.tld/gallery/image-003.jpg"
)
//--&gt;&lt;!]]&gt;
&lt;/script&gt;
&lt;/div&gt;

I changed the preload() array elements to valid images

The problem seems to be that there are TWO objects named preload()!!!

The first object is a function that should create the image elements.

The second object is 'preload( files )' and appears to be an array of files to be displayed (changed to valid links).

How can this be??? ?

I also added 3 <img...> elements in the <body> because nothing seemed to display, but that did nothing either.

How would I reference the <img...> tags? Would I need to hard-code the <img> tags?


Or could they be generated dynamically to match the number of elements of the 'images' array?

Bottom line is that I think I don't understand what I'm doing at this point in time!
Copy linkTweet thisAlerts:
@Logic_AliApr 06.2012 — I changed the preload() array elements to valid images

The problem seems to be that there are TWO objects named preload()!!![/quote]

The first is a function, the second is a call to that function.

I also added 3 <img...> elements in the <body> because nothing seemed to display, but that did nothing either.

How would I reference the <img...> tags? Would I need to hard-code the <img> tags? [/quote]
This code isn't about displaying images, it's about having them downloaded and in memory, ready to be displayed when their filename is specified by a script.

I thought you knew all this of old.
Copy linkTweet thisAlerts:
@JMRKERauthorApr 06.2012 — The first is a function, the second is a call to that function.

This code isn't about displaying images, it's about having them downloaded and in memory, ready to be displayed when their filename is specified by a script.

I thought you knew all this of old.[/QUOTE]


That was the concept problem I was talking about in the 1st post.

I think it is beginning to gel. :o
×

Success!

Help @JMRKER 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 4.29,
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,
)...