Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-28-2011, 12:21 AM   PM User | #1
oliarcher
New to the CF scene

 
Join Date: Jun 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
oliarcher is an unknown quantity at this point
Multiple random link generators on one page

Hi, I'm currently trying to incorporate several random link generators on one page, each with different links to be directed to. However, at the minute all the links just direct me to the URLs of the most recent random link to be created. If someone could point me in the right direction to fixing this that would be great! The code I am currently using is:


Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
var movie=new Array()
movie[0]="http://bringbacktheoc.weebly.com"
movie[1]="http://allaboutkarl.weebly.com"
movie[2]="http://recipeshareonline.weebly.com"

function randomLinks(elem)
{
	var randomLink=Math.floor(Math.random()*2);
	elem.href=movie[randomLink];
	elem.target = "_blank";
}
</script>
<a href="#" onclick="randomLinks(this)"<p><font size="20" > 1 </font><p> </a>
</body>
</html>
Thanks!
oliarcher is offline   Reply With Quote
Old 06-28-2011, 02:22 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question Huh? ...

Quote:
Originally Posted by oliarcher View Post
Hi, I'm currently trying to incorporate several random link generators on one page, each with different links to be directed to. However, at the minute all the links just direct me to the URLs of the most recent random link to be created. If someone could point me in the right direction to fixing this that would be great! The code I am currently using is:

...
Your request is not very clear.

You only have 3 possible links to go to randomly.
With so few links to go to, you will have a 33% chance of going to the same link each time you click the link. That is just the nature of the random function.

Are you trying to randomly go to a different link when you click on one of the 3 links available? Is that what you are trying to accomplish? Are you trying to guarantee that the 3 links will go to a random URL one time before repeating a random sequence again?
jmrker is offline   Reply With Quote
Old 06-28-2011, 10:31 AM   PM User | #3
oliarcher
New to the CF scene

 
Join Date: Jun 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
oliarcher is an unknown quantity at this point
Sorry about the confusing request, I'm a bit of a novice I'm afraid. I literally just mean I want several different random links on one page.

e.g.
Link 1 --> RandomURL A
RandomURL B
RandomURL C

Link 2 --> RandomURL D
RandomURL E
RandomURL F

etc.

At the minute using that code all that happens is that all the links direct to whichever code I input most recently. So for the above example, Link 1 would still direct to RandomURLs D, E and F. Just wondering if there was something I could do to rectify this?
oliarcher is offline   Reply With Quote
Old 06-28-2011, 03:21 PM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb Something to try...

Try this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
var movie = [
    "http://bringbacktheoc.weebly.com",
    "http://allaboutkarl.weebly.com",
    "http://recipeshareonline.weebly.com"  // No comma after last entry
];
var forums = [
    "http://www.webdeveloper.com",
    "http://www.codingforums.com",
    "http://www.wdroom.com",
    "http://dreamincode.net"  // No comma after last entry
];
function randomLinks(elem, tarr) {
  var randomLink=Math.floor(Math.random()*tarr.length);
// alert(tarr[randomLink]);  // for testing purposes only
  elem.href = tarr[randomLink];
  elem.target = "_blank";
}
</script>
<a href="javascript:void(0)"
 onclick="randomLinks(this,movie)" style="font-size:2em"> Movie </a>
<p>
<a href="javascript:void(0)"
 onclick="randomLinks(this,forums)" style="font-size:2em"> Forums </a>
</body>
</html>
You can add to either array without changing the JS sections a bit.

Good Luck!
jmrker is offline   Reply With Quote
Reply

Bookmarks

Tags
generator, link, multiple, page, random

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 PM.


Advertisement
Log in to turn off these ads.