By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Image Preload With A For Loop

P: n/a
This isn't working and I can't figure out what I'm doing wrong:

var c_imgs = new Array(
'images/champascari.jpg',
'images/champfangio.jpg',
'images/champhawthorn.jpg',
'images/champhill.jpg',
'images/champsurtees.jpg',
'images/champlauda.jpg',
'images/champschumacher.jpg');
var pre_img;
for (var i = 0; i < c_imgs.length; i++)
{
pre_img[i] = new Image();
pre_img[i].src = c_imgs[i];
}

If this code is correct, should I put it in a function and then call for
it ?

This is the page it's on:
http://www.xlectric.com/Ferrari/champs.html

Later, Art.

Dec 8 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
X l e c t r i c wrote on 08 dec 2005 in comp.lang.javascript:
This isn't working and I can't figure out what I'm doing wrong:

var c_imgs = new Array(
'images/champascari.jpg',
'images/champfangio.jpg',
'images/champhawthorn.jpg',
'images/champhill.jpg',
'images/champsurtees.jpg',
'images/champlauda.jpg',
'images/champschumacher.jpg');
var pre_img;
var pre_img = new Array();
for (var i = 0; i < c_imgs.length; i++)
{
pre_img[i] = new Image();
pre_img[i].src = c_imgs[i];
}

If this code is correct, should I put it in a function and then call for
it ?

This is the page it's on:
http://www.xlectric.com/Ferrari/champs.html

Later, Art.


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Dec 8 '05 #2

P: n/a
Evertjan wrote:

"var pre_img = new Array();"

Thank you Evertjan, it works great. I appreciate your help.

Is that pre_img array for the preload only ? For example if I want to
call for an image later on using the array, would I use the original
c_imgs array, like this (for the first image in the array):

c_imgs[0]

or would I use:

pre_img[0]

Once again, thanks.

Later, Art.

Dec 8 '05 #3

P: n/a
Xl******@webtv.net (X l e c t r i c) writes:
This isn't working and I can't figure out what I'm doing wrong:

var c_imgs = new Array(
'images/champascari.jpg',
'images/champfangio.jpg',
'images/champhawthorn.jpg',
'images/champhill.jpg',
'images/champsurtees.jpg',
'images/champlauda.jpg',
'images/champschumacher.jpg');
var pre_img;
pre_img is uninitialized, so it contains the value "undefined".
for (var i = 0; i < c_imgs.length; i++)
{
pre_img[i] = new Image();
Here you try to find the 0th property of it, which fails with an
error.

You should make sure to show all errors while developing a
page. Depending on which browser you use, the way to show errors
differ (in IE, it's somewher in advanced internet properties, in
Mozilla/Netscape based browsers, enter "javascript:" in the address
bar).

If this code is correct, should I put it in a function and then call for
it ?


If it should be executed right then and there, there is no reason to
wrap it in a function, except perhaps to hide the local variables ...
which is actually reason enough, so yes, do that.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dec 9 '05 #4

P: n/a
Lasse*Reichstein*Nielsen wrote:

"pre_img is uninitialized, so it contains the value "undefined".

for (var i = 0; i < c_imgs.length; i++)
{
pre_img[i] = new Image();

Here you try to find the 0th property of it, which fails with an error.

You should make sure to show all errors while developing a page.
Depending on which browser you use, the way to show errors differ (in
IE, it's somewher in advanced internet properties, in Mozilla/Netscape
based browsers, enter "javascript:" in the address bar).

If this code is correct, should I put it in a function and then call for
it ?

If it should be executed right then and there, there is no reason to
wrap it in a function, except perhaps to hide the local variables ...
which is actually reason enough, so yes, do that."

I realize that now since Evertjan pointed out the use of the array.

I appreciate you teaching me how to show errors in the browsers, I did
not know how to do that.

Thank you very much for your help.

Later, Art.

Dec 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.