Solved

Adding a Link to random image array list in java

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

688 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