Solved

Adding a Link to random image array list in java

Posted on 2010-09-10
4
304 Views
Last Modified: 2013-11-23
I have little to no experience in Java, and I have been asked to link each picture produced by this script to page called "promo.html". Is there a way to just add something to the existing script to accomplish this?

// JavaScript Document

/*
usage:
make sure to use the same id and then add all the randon images in the array list below.
<img class="mainBg" id="randPic" src="img/bg_homemain1.jpg" alt="">
*/
	//had to disable this because of a confliction with another script.
	//window.onload = choosePic;
	//this chooses a random pic for the main page.
	var myPix = new Array("images/Rendering-Home-option-1.jpg","images/Rendering-Home-option-2.jpg","images/Rendering-Home-option-3.jpg");
	
	function choosePic()
	{
		randomNum=Math.floor((Math.random() * myPix.length));
		document.getElementById("randPic").src=myPix[randomNum];
	}

Open in new window

0
Comment
Question by:Yensign
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33650768
So what you want to do is, at the click event of any image, it has to go to that html page

make it


document.getElementById("randPic").onclick=function(){
   location.href = "promo.html";
}

function choosePic()
      {
            randomNum=Math.floor((Math.random() * myPix.length));
            document.getElementById("randPic").src=myPix[randomNum];
      }
0
 
LVL 2

Accepted Solution

by:
kostantinos1995 earned 250 total points
ID: 33650770
Just add the following line in the function:
document.getElementById("randPic").onclick= function event(onclick) {window.location="promo.html";};

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33650790
another way to do it is

function goToPromo()
{
   location.href = "promo.html";
}

document.getElementById("randPic").addEventListener("click",goToPromo,false);


http://www.javascriptkit.com/dhtmltutors/domevent3.shtml

0
 

Author Comment

by:Yensign
ID: 33651029
Perhaps I was just getting the code in the wrong place, but I was not able to get gurvinder's code to work. I was able to get kostantinos' to work on the first try. I will award the points there. Thank you very much for your help, both of you.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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