Webman04
asked on
Put several JavaScript Arrays on the same page so they each show a different image on load
Hi, I have the script below which works fine but I need to put it on the same page several times and have each script load separate images. What do I need to alter on each script to make several of them work on each page. Please be as clear as possible, thanks.
<script type="text/javascript">
//The array which is going to hold the image information. Store the image info along with their correct path; I've omitted the path here as I've stored the images in the same folder where my web page resides.
var imageArray = new Array();
imageArray[0] = Array("images/HomeSlides/image1.jpg", "link1.html");
imageArray[1] = Array("images/HomeSlides/image2.jpg", "link2.html");
imageArray[2] = Array("images/HomeSlides/image3.jpg", "link3.html");
imageArray[3] = Array("images/HomeSlides/image4.jpg", "link4.html");
imageArray[4] = Array("images/HomeSlides/image5.jpg", "link5.html");
imageArray[5] = Array("images/HomeSlides/image6.jpg", "link6.html");
imageArray[6] = Array("images/HomeSlides/image7.jpg", "link7.html");
function doIt()
{
var rand = Math.floor(Math.random()*7); //Generating a random number between 0 and 3 here in your case you can replace 4 with 11 if you have 10 images
var imgPath = "<a href='"+imageArray[rand][1]+"'><img src='"+imageArray[rand][0]+"' alt='' border='0' align='absmiddle' /></a>";
document.getElementById("image").innerHTML = imgPath;
}
</script>
ASKER
Hi, thanks the script is called with the code below, so would I just change the doIt(); to doIt0();, doIt1(); and so on?
<div align="center">
<div id="image"></div>
<script type="text/javascript">
doIt();
</script>
Yes, that should do it ;)
ASKER
Hi, I installed the script and setup the first Array0 but it won't work. Here is the link: http://72.29.5.13/defaulttest.asp
and here is the link of the page that I had before that works, in case this helps: http://72.29.5.13/default.asp
Note that I copy pasted it but corrected the image path. Do I have to add all of the images for each array before I can test it? or will it work on just the first one and then I can test it? Please take a look and let me know what I'm doing wrong.
Thanks very much for your help!!!!
and here is the link of the page that I had before that works, in case this helps: http://72.29.5.13/default.asp
Note that I copy pasted it but corrected the image path. Do I have to add all of the images for each array before I can test it? or will it work on just the first one and then I can test it? Please take a look and let me know what I'm doing wrong.
Thanks very much for your help!!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh yeah, I forgot to tell you that you should be able to get them working one at a time as long as the code is proper. The error on the original we worked on was just "rand is not defined" because we changed the rand variable declaration to "var rand0 = " but we were still calling for "rand" when we defined "imgPath0", instead of calling for "rand0".
var imgPath0 = "<a href='"+imageArray0[rand][1]+"'><img src='"+imageArray0[rand][0]+"' alt='' border='0' align='absmiddle' /></a>";
var imgPath0 = "<a href='"+imageArray0[rand][1]+"'><img src='"+imageArray0[rand][0]+"' alt='' border='0' align='absmiddle' /></a>";
ASKER
Thanks very much, this works great.
ASKER
Just so you know it works great, but I had to use several image references, one for each image, total of 5 image boxes on the page so I will use image, image1, image2... and then I can attach different style sheets if I need different size images in each. For some reason it would not work until I did this? Jut thought I'd let you know in case anyone else needs the same script. Thanks very much for your amazing skills and help.
Thanks
Thanks
var imageArray = new Array ()
imageArray[0]
imageArray[1]
function doIt()
var imgPath = "<a href='"+imageArray[rand][1
document.getElementById("i
---- becomes ----
var imageArray0 = new Array();
imageArray0[0] = ...;
imageArray0[1] = ...;
function doIt0()
var rand0 = Math.floor(Math.random()*7
var imgPath0 = "<a href='"+imageArray0[rand0]
document.getElementById("i
var imageArray1 = new Array();
imageArray1[0] = ...;
imageArray1[1] = ...;
function doIt1()
var rand 1= Math.floor(Math.random()*7
var imgPath1 = "<a href='"+imageArray1[rand1]
document.getElementById("i
etc....
You did not include how the script is called so I hope the attached code description above is enough. Don't forget to change each script call for each image array.
Open in new window