random page link question

G

groucho

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 text
link and come up with, for e.g.: p2, p2, p3, p1, p2, p2, p2, p1 etc...

Thanks

Matt

Please respond to the group not email
 
L

Lee

(e-mail address removed) said:
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 text
link and come up with, for e.g.: p2, p2, p3, p1, p2, p2, p2, p1 etc...


<html>
<head>
<script type="text/javascript">
p = [
"http://www.myDomain/p1.html",
"http://www.myDomain/p2.html",
"http://www.myDomain/p3.html",
"http://www.myDomain/p4.html",
"http://www.myDomain/p5.html"
];
function randomLink(list) {
location.href=p[Math.floor(Math.random()*p.length)];
}
</script>
</head>
<body>
<p>
If Javascript is enabled, this link will go to a page selected
randomly from the URLs contained in array &quot;p&quot;.<br>
If Javascript is not enabled, the link will go to
&quot;p1.html&quot;.
</p>
<a href="http://www.myDomain/p1.html"
onclick="randomLink(p);return false">go</a>
</body>
</html>
 
S

Stephen Chalmers

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 text
link and come up with, for e.g.: p2, p2, p3, p1, p2, p2, p2, p1 etc...

A simple solution might be:

<A href='default.htm' onclick='return randPage(myPages)'>Go...?</A>

<script type='text/javascript'>

var myPages=[ .., .., .. ];

function randPage(pages)
{
location = pages[ Math.floor(Math.random()*pages.length) ];

return false;
}

</script>

However with a small number of pages, there is a high probability that a given user will be sent to the same page on
possibly several consecutive occasions, and will perceive no randomness. Therefore it may be better where possble to use
a cookie to record previous selections, to exclude them from the range until all have been selected.
 
G

groucho

Works a treat - thanks :)

--Matt

---

(e-mail address removed) said:
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 text
link and come up with, for e.g.: p2, p2, p3, p1, p2, p2, p2, p1 etc...


<html>
<head>
<script type="text/javascript">
p = [
"http://www.myDomain/p1.html",
"http://www.myDomain/p2.html",
"http://www.myDomain/p3.html",
"http://www.myDomain/p4.html",
"http://www.myDomain/p5.html"
];
function randomLink(list) {
location.href=p[Math.floor(Math.random()*p.length)];
}
</script>
</head>
<body>
<p>
If Javascript is enabled, this link will go to a page selected
randomly from the URLs contained in array &quot;p&quot;.<br>
If Javascript is not enabled, the link will go to
&quot;p1.html&quot;.
</p>
<a href="http://www.myDomain/p1.html"
onclick="randomLink(p);return false">go</a>
</body>
</html>
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top