...

View Full Version : Random Linked Image



liberal123
12-07-2004, 03:48 PM
ok, i have a code for a random 80x80 image ot appear in a table, but i need it to be a random 80x80 linked image, here is my code at the moment

The Part That Goes In The Head,

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'images/cars/focus.jpg'
theImages[1] = 'images/cars/ka.jpg'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

// End -->
</script>

And Then The Part That Goes In The Table,

<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
showImage();
// End -->
</script>

Any Help Would Be VERY VERY Much Appreciated

Willy Duitt
12-07-2004, 04:50 PM
It's already random...
var whichImage = Math.round(Math.random()*(p-1));

All you need to do is add more images to the array following the example and the comments regarding images to the array...


// To add more image files, continue with the
// pattern below, adding to the array.


.....Willy

liberal123
12-07-2004, 05:09 PM
i know its random, but i need the pictures to be links aswell. at the moment they are random pictures, but i want them to be random LINKED pictures

Willy Duitt
12-07-2004, 05:16 PM
Oh, I misread your post and only noticed multiple references to random and missed the lone reference to LINKED which was lost in all the retoric...

Try using a two demensional array:

theImages[0] = [ 'images/cars/focus.jpg', 'http://link.html' ];

And:
document.write('<a href="'+theImages[whichImage][1]+'"><img src="'+theImages[whichImage][0]+'"></a>');

.....Willy

BTW: I question the need to preload all the images in the array when you are only using the one image which will be loaded when the page loads anyway... Seems counter productive in that this will stall the page load while the preload downloads and caches all of these unneeded images...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum