473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 1672
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Papa Legba | last post by:
I have huge page of links, to whcih I am currently adding. I would like to add a "random link" button which would parse the current page, extract all URLs then load a random link. Before I start...
12
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
1
by: Eric | last post by:
I'm a new programmer on Visual Basic and for any of you who play tank wars, I'm attempting to lay out a terrain similar to it. What I need is a terrain that I can draw (Basically just a random...
3
by: Heath | last post by:
Using MSIE 5+ Heath writes: My problem deals with working with window objects between pages as follows: My Introduction page contains a link to my Action page. The onClick of that link...
6
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!--...
3
by: groucho | last post by:
I want to click on text and have it open a different *random* webpage every time from a list of urls I have already provided. So if I have 3 pages - p1, p2, p3 - i want to be able to click the...
9
by: Stu Banter | last post by:
Hi, On a previous question someone recommended using the System.Security.Cryptography to fill an array with strong random bytes. It works but I can't specify the max value of course.. I solved...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
4
by: rossum | last post by:
I am looking for a source for the internal details of the System.Random pseudo random number generator. Something reasonably technical please, not how to use it (which I know) but more how does...
16
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.