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

random link form current page

P: n/a
I have huge page of links, to whcih I am currently adding. I would
like to add a "random link" button which would parse the current page,
extract all URLs then load a random link.

Before I start to code, does such s thing already exist.

Thanks in advance...
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi, such scripts already exist for the most purposes.
Displaying random quotes, links, images, etc.
A start is to look through the category: Randomizing, at hotscripts.com
http://www.hotscripts.com/PHP/Script...ing/index.html

Another tip is to google for something like: PHP Code Display random link
http://www.google.com/search?hl=en&l...nk&btnG=Search

Most scripts I've come across use a mysql database as background,
but if you search enough, you'll find scripts that use normal text-files as base.
You'll most likely need to reorganize you link collection, in order to make
it work "out of the box" with finished scripts.

-Geir
"Papa Legba" <Pa************@gmail.com> wrote in message news:39**************************@posting.google.c om...
I have huge page of links, to whcih I am currently adding. I would
like to add a "random link" button which would parse the current page,
extract all URLs then load a random link.

Before I start to code, does such s thing already exist.

Thanks in advance...

Jul 17 '05 #2

P: n/a
Ivo
"Geir Andersen" wrote
"Papa Legba" wrote
I have huge page of links, to whcih I am currently adding. I would
like to add a "random link" button which would parse the current page,
extract all URLs then load a random link.

Hi, such scripts already exist for the most purposes.
Displaying random quotes, links, images, etc.
A start is to look through the category: Randomizing, at hotscripts.com
http://www.hotscripts.com/PHP/Script...ing/index.html

Most scripts I've come across use a mysql database as background,
but if you search enough, you'll find scripts that use normal text-files
as base.


I don't see the need for anything serverside, especially not for databases,
when the page is already in the browser. These two lines of javascript do
the job perfectly on any page:

var a = document.links;
location.href = a[ Math.floor( a.length*Math.random() ) ];

If the page contains lots of '#' or 'mailto' or other strange links, you may
want to go the extra mile and filter them out:

var a = document.links, x ;
while ( !x || x.href.indexOf('http') !== 0 ) {
x = a[ Math.floor( a.length * Math.random() ) ];
}
location.href = x;

hth
--
Ivo




Jul 17 '05 #3

P: n/a
"Ivo" <no@thank.you> wrote in message news:<422737f3$0$9747
I don't see the need for anything serverside, especially not for databases,
when the page is already in the browser. These two lines of javascript do
the job perfectly on any page:

var a = document.links;
location.href = a[ Math.floor( a.length*Math.random() ) ];

If the page contains lots of '#' or 'mailto' or other strange links, you may
want to go the extra mile and filter them out:

var a = document.links, x ;
while ( !x || x.href.indexOf('http') !== 0 ) {
x = a[ Math.floor( a.length * Math.random() ) ];
}
location.href = x;


Don, that seems incredible. I have almost hacked it in PHP, but it is
much longer. Can you tell me how to refernce that from HTML (sorry, I
am not a JS person). Thanks again.
Jul 17 '05 #4

P: n/a
Ivo
"Papa Legba" wrote
Don, that seems incredible. I have almost hacked it in PHP, but it is
much longer. Can you tell me how to refernce that from HTML (sorry, I
am not a JS person). Thanks again.


<script type="text/javascript">
function randomlink() {
var a = document.links, x ;
while ( !x || x.href.indexOf('http') !== 0 ) {
x = a[ Math.floor( a.length * Math.random() ) ];
}
location.href = x;
return false;
}
</script>

<p><a href="#" onclick="return randomlink();">Random link</a></p>

There is one caveat: on a page with nothing but "#" links the 'while' loop
will never end, freezing your browser, watch out that. For example, don't
try the above in a blank document like I did, but be sure to include some
links too :))
--
Ivo


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.