PDA

View Full Version : arrays


chris_angell
07-02-2002, 12:31 PM
hello, i am trying to call new images from my js code, but the trouble I am having is ::: i want to add an array of values so i can call different images into different areas.. at the moment I have done it like this... just repeating the code

var preload = new Image();
preload.src = "../images/leftblock_bot.gif";

function GIFS() {
document['botleft'].src = preload.src;
}
var preload = new Image();
preload.src = ".../images/top_blockleft.gif";

function GIFS() {
document['mainleft'].src = preload.src;
}

but this is long and sloppy, is there an easier way to say i want this named id to call this image using more than one var ??

thank you chris

:(

joh6nn
07-02-2002, 12:48 PM
function gifSwitcher(image, source) {
document.images[image].src = source;
}
var pics = [ ['botleft', "../images/leftblock_bot.gif"], ['mainleft', "../images/top_blockleft.gif"] ];

for ( var i = 0; i < pics.length; i++) {
gifSwitcher(pics[i][0], pics[i][1]);
}


i think that's what you were asking for. it makes the function gifSwitcher, which takes the argument of which image to switch, and the picture it should be switched to. then, you create a 2 column array. the first column is the name of the picture to switch, and the second column is the source of the picture to switch to. then, using a for-next loop, you run through this entire array, assigning its contents to the gifSwitcher.