473,473 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Image Preload With A For Loop

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
4 1382
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephane Vander Clock | last post by:
hello, i use this code to preload image : <script language="JavaScript"> <!-- function preloadimages() { var d=document; if(d.images){ if (!d.preload) { d.preload=new Array(); }
1
by: Peter Fastré | last post by:
Hello The javascript preload function I always used, is causing problems with the latest version of gecko-browsers. Never had any problems with it though. Maybe there's something wrong? ...
4
by: David | last post by:
Hi everyone, I am trying to stop an image preload sequence by the click of a mouse but have been unsuccessful trying several methods. Imagine this simple script below that loads 50 images to...
2
by: bedges | last post by:
okay, the scenario: i have a header image which changes randomly across all pages in the site. that works fine. i also have an image preloader within the random header picker which theoretically...
2
by: sachaburnett | last post by:
Hi everyone! I'm new to Javascript and am finding so much useful information on this group, so thanks to you all! I have a question about preloading images for onmouseover/out effects and...
4
by: tetris | last post by:
I have this little code wich is working quite good, it makes a slide show on the body background, anyone could guide me on how to add a fade effect as a transition between the images?? var...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.