Solved

Adding a Link to random image array list in java

Posted on 2010-09-10
4
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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