Solved

Adding a Link to random image array list in java

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now