Link to home
Start Free TrialLog in
Avatar of Webman04
Webman04Flag for United States of America

asked on

add image link to JavaScript array

Hi, I have posted the script below.  This works fine to show a random image but I need to know how to link each image when it's shown?  I want to allow the user to click on the image shown and be take to a url.  How to I connect a link to each image in the array?
<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] = "images/HomeSlides/image1.jpg"; //You can replace these image file names with your own image names.
imageArray[1] = "images/HomeSlides/image2.jpg";
imageArray[2] = "images/HomeSlides/image3.jpg";
imageArray[3] = "images/HomeSlides/image4.jpg";
imageArray[4] = "images/HomeSlides/image5.jpg";
imageArray[5] = "images/HomeSlides/image6.jpg";
imageArray[6] = "images/HomeSlides/image7.jpg";
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 = "<img src='"+imageArray[rand]+"' alt='' border='0' align='absmiddle' />";
 
document.getElementById("image").innerHTML = imgPath;
 
}
</script>
 
 
 
 
 <script type="text/javascript">
doIt();
    </script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Phatzer
Phatzer
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Webman04

ASKER

Perfect, thanks very much for your fast and accurate response!!!