?
Solved

add image link to JavaScript array

Posted on 2009-04-17
2
Medium Priority
?
354 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Webman04
2 Comments
 
LVL 10

Accepted Solution

by:
Phatzer earned 2000 total points
ID: 24169119
As such I believe, give it a whirl:
<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>
 

Open in new window

0
 

Author Closing Comment

by:Webman04
ID: 31571529
Perfect, thanks very much for your fast and accurate response!!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question