Solved

Adding a Link to random image array list in java

Posted on 2010-09-10
4
307 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

791 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