Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you create a link button in JavaScript?

Status
Not open for further replies.
Jan 22, 2004
18
US
I'm a JS newbie in dire need of advice for my something I'm working on for my senior project. I need to create a button that links to another website. That's usually a simple matter of filling in the "onclick" field with the website address, but the way I need to do it has a twist. Instead of doing it in straight HTML, I need JavaScript. Here is, in essence, what I'm trying to pull off:

First, I have an array in which each member of that array is a website, as such:

var links = new array(3);
links[0]="links[2]="links[3]="
Next, I have three buttons in my HTML in the body that I need to link to each website, respectively. So, what do I do with the "onclick" field so that it calls up the JavaScript above (I presume through a function I must create) and goes to one of the websites in the array?
 
Your array should be from 0-2 (3 values) - but that's probably a typo. [thumbsup2]

Try this:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
	<title>Link to Website</title>
<script type=&quot;text/javascript&quot;>
var links = new Array(3);
links[0]=&quot;[URL unfurl="true"]http://www.website1.com&quot;;[/URL]
links[1]=&quot;[URL unfurl="true"]http://www.website2.com&quot;;[/URL]
links[2]=&quot;[URL unfurl="true"]http://www.website3.com&quot;;[/URL]
function GoThere(num){
	location.href=links[num];
}
</script>
</head>
<body>
<button onClick=&quot;GoThere(0);&quot;>First Link</button>  
<button onClick=&quot;GoThere(1);&quot;>Second Link</button>  
<button onClick=&quot;GoThere(2);&quot;>Third Link</button>  
</body>
</html>

Hope this helps.

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top