469,936 Members | 2,422 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,936 developers. It's quick & easy.

Random Link

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,

--
Global_Killa
"You're a victim of the rules you live by!"
Jul 23 '05 #1
1 1591
Ivo
"Global_Killa" wrote
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

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Papa Legba | last post: by
12 posts views Thread by bhennon | last post: by
1 post views Thread by Eric | last post: by
3 posts views Thread by Heath | last post: by
3 posts views Thread by groucho | last post: by
9 posts views Thread by Stu Banter | last post: by
5 posts views Thread by Peteroid | last post: by
4 posts views Thread by rossum | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.