Click to See Complete Forum and Search --> : Random text link?
96turnerri
09-11-2003, 06:22 PM
hi PYRO gave me this script and it works fine thanks m8
<script type="text/javascript">
text = new Array("1","2","3","4","5");
document.write(text[parseInt(Math.random()*text.length)]);
</script>
does anybody know if it would be possible and if so how? i can make the random text a link so if '1' shows it links to say table.htm and '3' shows it links to chair.htm
help would be much appreciated
// Untested code...
var txt = new Array("One", "Two", "Three");
var links = new Array("1.html", "2.html", "3.html");
var rand = parseInt(Math.random()*(txt.length+links.length/2));
document.write("<a href="+links[rand]+">"+txt[rand]+"</a>");
[J]ona
Charles
09-12-2003, 04:37 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
Array.prototype.random = function () {return this[Math.floor(Math.random() * this.length)]}
// -->
</script>
<a href="default.html" onclick="this.href = ['couch.html', 'table.html', 'desk.html', 'chair.html'].random()">Random</a>
The parseInt() function takes a string, not a number. Pyro's way of doing things requires that the number returned by "Math.random()*Array.length)" be turned into a Number object so that Number.toString() can be called just so that it can be then converted back into a number. The Math.floor() method is what you want.
96turnerri
09-12-2003, 07:15 AM
// Untested code...
var txt = new Array("One", "Two", "Three"); var links = new Array("1.html", "2.html", "3.html");
var rand = parseInt(Math.random()*(txt.length+links.length/2));
document.write("<a href="+links[rand]+">"+txt[rand]+"</a>");
this is what Jona posted from this i get 4 displays 1, which links to 1.html and 2 and 3 which are same respectivly and undefined which links to undefined.html do you know why this is??
thanks
96turnerri
09-12-2003, 07:18 AM
charles thanks for your help but i cant get your way to work, each page displays a random 1,2,3,4,or 5 next to it the same link never changes
requestcode
09-12-2003, 08:23 AM
How about this example:
<html>
<head>
<title>Random Link</title>
</head>
<body>
<table align="center" border="0" width="200" height="100" cellspacing="0" cellpadding="0">
<tr align="center" valign="top">
<td align="center" valign="top">
<script language="JavaScript">
/* Enter the address and the name you want to show up when the link is displayed.
you must separate them with a comma or the script will not work correctly. You
also cannot use a comma in the address or name, this will also cause problems.
*/
var linkarr=new Array()
linkarr[0]="http://www.wsabstract.com,Website Abstraction"
linkarr[1]="http://www.dynamicdrive.com,Dynamic Drive"
linkarr[2]="http://www.requestcode.com,Request Code"
linkarr[3]="http://www.hotscripts.com,Hot Scripts"
linkarr[4]="http://www.htmlgoodies.com,HTML Goodies"
len=linkarr.length // how many entries in the array
randnm=Math.round(Math.random()*(len-1))
ranlink=linkarr[randnm].split(",") // separate the address and name
document.write("<a href='"+ranlink[0]+"' title='"+ranlink[1]+"'>"+ranlink[1]+"</a>")
document.close()
</script>
</td>
</tr>
</table>
<center>
<font size="-1" color=red>Click <a href="#" onClick="location.reload()">Reload</a> to see a different link</font>
<br><br><br>
<a href="#" onCLick="self.close()">Close Window</a>
</center>
</body>
</html>
96turnerri
09-12-2003, 08:27 AM
ok thanks im just off to work now, ill test it when i get back cheers m8 hope it works lol :D
Originally posted by 96turnerri
this is what Jona posted from this i get 4 displays 1, which links to 1.html and 2 and 3 which are same respectivly and undefined which links to undefined.html do you know why this is??
thanks
I said it was untested code, and I doubted that pyro's method of random, in this case, would work properly (as Charles stated). You should use Math.floor().
[J]ona
My method does work, though as Charles pointed out, it should use Math.floor, as we are not passing a string to it.
Shampie
09-12-2003, 12:07 PM
amount = 8; //set how many links you have..
rndm = Math.round(Math.random() * amount) % amount + 1 ;
location.replace("file"+rndm+".html");
Would this be any help?
// notives this is not what was requested, my apologies.
Charles
09-12-2003, 12:20 PM
Or you can use my method. It'll keep track of the array size for you. And yes, it works fine if you use it properly.
Array.prototype.random = function () {return this[Math.floor(Math.random() * this.length)]}
96turnerri
09-12-2003, 06:11 PM
ok guys thanks for ur help im not actually at home at the moment so i cant test it, some scripts posted only list to a random page, i want a different link for each different random display if that helps?