Closed Thread Icon

Topic awaiting preservation: Preloading an array with onload= (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=22798" title="Pages that link to Topic awaiting preservation: Preloading an array with onload= (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Preloading an array with onload= <span class="small">(Page 1 of 1)</span>\

 
flyttbil
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Aug 2004

posted posted 08-03-2004 16:19

I'm having trouble with preloading an array with image objects in it.
As seen by the code below, there are tow identical blocks of code (one being commented and one being the content of a function named preload() ).

The purpose of the script is to show a big picture when klicking on the corresponding small one. This works fine when removing the comments in front of the code below. I want it to work when preloading the array by calling the preload-function by using <body onload=...

Any suggestions?

<script language="javascript" type="text/javascript">
<!--

//var pics=new Array("k1.jpg", "k2.jpg", "k3.jpg", "k4.jpg");
//var pic=new Array();

//for(var i=0; i<pics.length; i++){
// pic[i]=new Image;
// pic[i].src=pics[i];
//}

function preload(){
var pics=new Array("k1.jpg", "k2.jpg", "k3.jpg", "k4.jpg");
var pic=new Array();

for(var i=0; i<pics.length; i++){
pic[i]=new Image;
pic[i].src=pics[i];
}
}

function change_image(i){
biggie.src=pic[i].src
}

-->
</script>

</head>

<body onload="javascriptreload();">
<body onload="javascriptreload();">
<img src="k1t.jpg" onClick="change_image(0)"/>
<img src="k2t.jpg" onClick="change_image(1)"/>
<img src="k3t.jpg" onClick="change_image(2)"/>
<img src="k4t.jpg" onClick="change_image(3)"/>

<br/><br/>
<img src="k1.jpg" alt="The Man" name="biggie"/>

</body>

BillyRayPreachersSon
Nervous Wreck (II) Inmate

From: London
Insane since: Jul 2004

posted posted 08-04-2004 00:01

You have two body tags - try removing one of them and seeing if it still fails.

Hope this helps,
Dan

flyttbil
Obsessive-Compulsive (I) Inmate

From:
Insane since: Aug 2004

posted posted 08-04-2004 10:30

no, that was just a stupid misstake when copy-pasting.
i figured out what was wrong. thanks for the help though.

BillyRayPreachersSon
Nervous Wreck (II) Inmate

From: London
Insane since: Jul 2004

posted posted 08-04-2004 11:23

What was it? The missing () after "new Image"?

Dan

flyttbil
Obsessive-Compulsive (I) Inmate

From:
Insane since: Aug 2004

posted posted 08-06-2004 13:27

No, I had to initialize the arrays outside preload(). Otherwise change_image() won't be able to acsess them. It seems like this solution only works for IE.

BillyRayPreachersSon
Nervous Wreck (II) Inmate

From: London
Insane since: Jul 2004

posted posted 08-06-2004 17:17
quote:
It seems like this solution only works for IE.



if you change this:

code:
var pics=new Array("k1.jpg", "k2.jpg", "k3.jpg", "k4.jpg");



to this:

code:
var pics=['k1.jpg', 'k2.jpg', 'k3.jpg', 'k4.jpg'];



And then put the () after all occurrences of "new Image" (to make "new Image()"), does that make any difference to the cross-browser issues?

Hope this helps,
Dan

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 08-06-2004 19:04

new Array() should be just as valid as [].

However, I do think that putting the () after new Image should fix it.

Don't forget the semicolon after biggie.src=pic[i].src as well (not required, but nice ).

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

InI
Maniac (V) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 08-06-2004 23:01

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

BillyRayPreachersSon
Nervous Wreck (II) Inmate

From: London
Insane since: Jul 2004

posted posted 08-07-2004 11:21
quote:
new Array() should be just as valid as [].



Absolutely - The only reason I advise people to steer clear of this syntax:

code:
var pics=new Array("k1.jpg", "k2.jpg", "k3.jpg", "k4.jpg");



is because it doesn't work for all cases - for example, when you want to initialise an array with one number, doing this

code:
var myArray=new Array(10);



doesn't work as you might expect if you're not aware of these things

Dan

InI
Maniac (V) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 08-07-2004 12:22

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

« BackwardsOnwards »

Show Forum Drop Down Menu