Random Link

G

Global_Killa

Hey all,
I'm new to java script, and really don't know anything about it. I'm
looking to have a random link shown on my site. I want the link to be chosen
from a list of about 10 links.
How would I go about this? I found this piece of code, but this only allows
two links. Does anyone know how to expand on this to add 10 or more?

<script type="text/javascript">
var r=Math.random()
if (r>0.5)
{
document.write("<a href='http://www.w3schools.com'>Learn Web
Development!</a>")
}
else
{
document.write("<a href='http://www.refsnesdata.no'>Visit Refsnes
Data!</a>")
}
</script>

Thanks for any help,
 
I

Ivo

I'm new to java script, and really don't know anything about it. I'm
looking to have a random link shown on my site. I want the link to be chosen
from a list of about 10 links.
How would I go about this? I found this piece of code, but this only allows
two links. Does anyone know how to expand on this to add 10 or more?

<snip>

There are many ways to do this. Close to your original code, but allowing
for an arbitrary number of links, is this:

thelinks=['www.yahoo.com/','www.google.com/','www.c.com/','www.d.com/'];
thetexts=['Yahoo','Google','c','d'];
a=Math.floor(thelinks.length*Math.random());
document.write('<a href="http://'+thelinks[a]+'">'+thetexts[a]+'</a>');

It should be mostly self-explanatory, if it isn't, let us know.
HTH
Ivo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top