469,366 Members | 2,313 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

random page link question

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

Aug 30 '05 #1
3 1514
Lee
gr*****@mockfrog.com 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>

Aug 30 '05 #2
<gr*****@mockfrog.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...
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.

--
S.C.

Aug 30 '05 #3
Works a treat - thanks :-)

--Matt

---
Lee wrote:
gr*****@mockfrog.com 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>


Aug 30 '05 #4

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
3 posts views Thread by Heath | last post: by
1 post views Thread by sven.daems | last post: by
3 posts views Thread by John Fairhurst | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.