PDA

View Full Version : Problem with Image Preload Please Help!



krusty
10-05-2006, 01:45 PM
Hi everybody,

i have problems with image preloading. I have written the following code.


<script type="text/javascript" language="JavaScript">
<!--
var myimages=new Array();
function preloadimages()
{
for (i=0;i<allpicture.arguments.length;i++)
{
myimages[i]=new Image()
myimages[i].src=allpicture.arguments[i]
}
}

var allpicture = new Array();
allpicture("/icons/system.gif","/icons/walk.bmp","/icons/start.jpg","/icons/stop.bmp");
//-->
</script>

Into the body tag i have witten this:


<body onLoad="preloadimages()" bgcolor="#E0E0E0" >

But this function doesn't work. My firefox browser returns the following errors:

Error: allpicture is not a function
Error: allpicture.arguments has no properties

Have anybody an idea what the problem is? I'm not a good javascript coder, but i need this feature. Please help.:(

Sorry for my bad english. :rolleyes:

codeexploiter
10-05-2006, 03:37 PM
I've modified your code a bit, you can find the difference between them easily



<script type="text/javascript" language="JavaScript">
<!--
var myimages=new Array();

function preloadimages()
{
for (i=0;i<allpicture.length;i++)
{
myimages[i]=new Image();
myimages[i].src=allpicture[i];
alert(myimages[i].src);

}
}

var allpicture = new Array("/icons/system.gif","/icons/walk.bmp","/icons/start.jpg","/icons/stop.bmp");
//-->
</script>


You can remove the alert() that i've used within the for loop kept it just for testing purpose.

Twey
10-05-2006, 06:55 PM
language="JavaScript"The language attribute is deprecated.
<!--
...
//-->Unnecessary. The only reason this would be necessary is if you had reserved characters in the script (such as < or >), in which case the script should be included externally.

codeexploiter's code will work (EDIT: sorry, won't work: preloadimages() is never called), but is more verbose than it has to be. Try:
<script type="text/javascript">
function preloadImages() {
for(var i = 0, s = preloadImages.store; i < arguments.length; ++i)
(s[s.length] = new Image()).src = arguments[i];
}
preloadImages.store = [];

preloadImages("/icons/system.gif","/icons/walk.bmp","/icons/start.jpg","/icons/stop.bmp");
</script>

codeexploiter
10-06-2006, 04:19 AM
Twey i think you missed the second part of Krusty's post. He called the function in the onLoad event of body. I just rectified erroneous function thats all rest he has to do.



Originally posted by: Krusty
Into the body tag i have witten this:


<body onLoad="preloadimages()" bgcolor="#E0E0E0" >

Twey
10-06-2006, 02:02 PM
Ah, OK.