http://www.dynamicdrive.com/forums/a...p/t-17207.html
Hi there,
I've been using this piece of javascript to load an image randomly from an array:
I have the script linked to an HTML file, and an id is linked on an img tag like this:Code:window.onload = function() { var randomImages = [ "img/logo1.gif", "img/logo2.gif", "img/logo3.gif", ]; document.images['random_image'].src = randomImages[Math.floor(Math.random() * randomImages.length)]; };
and it works fine for the topmost level of my site, but when i try link the same script into an HTML file that is down one level in a folder, the randomizer does not work. I figured out that each image has to go up one level to find an image and then bring it in, like this:Code:<img id="random_image" src="img/logo1.gif" width="700" height="252" border="0" />
If I were two levels down it would be:Code:window.onload = function() { var randomImages = [ "../img/logo1.gif", "../img/logo2.gif", "../img/logo3.gif", ]; document.images['random_image'].src = randomImages[Math.floor(Math.random() * randomImages.length)]; };
and so on. This means I have to have three separate javascript files. Does anyone know of a way to just have the one, but target all images?Code:"../../img/logo1.gif",
Thanks guys![]()



Reply With Quote

Bookmarks