View Single Post
  #2 (permalink)  
Old January 7th, 2005, 05:11 AM
joefawcett's Avatar
joefawcett joefawcett is offline
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

You have made a simple piece of code very cumbersome, why use:
preloadImage = function(...
Why not:
function preloadImg(...
Why use eval? NEVER use eval, each time you call it it starts a new script engine, I've only ever once seen the need for this function and that was in an app to teach JavaScript so that the app could run the student's dynamic code to see if it worked.

Why have
if (document.images)
before your script? You don't use the document.images collection so why test for it?

Your function also only has two arguments, imgObj and imgSrc, yet you are passing more than two parameters to it, thee others are ignored.

You are also trying to use variables such as 1, 2 and 3. Variables must start with a letter or underscore (I think this is the reason for the error message you are getting).

I would rewrite your code as:
function preloadImages(ImageSources)
  for (var i = 0; i < ImageSources.length; i++)
    var oImage = new Image();
    oImage.src = ImageSources[i];
and then pass an array of sources (note square brackets) to the function:
<body onLoad="preloadImages(['image_bin/1_over.gif', 'image_bin/2_over.gif', 'image_bin/'3_over.gif', 'image_bin/4_over.gif' ,'image_bin/5_over.gif'])">


Joe (Microsoft MVP - XML)