By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

random page link question

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
<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

P: n/a
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.