random link form current page

P

Papa Legba

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...
 
G

Geir Andersen

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/Scripts_and_Programs/Randomizing/index.html

Another tip is to google for something like: PHP Code Display random link
http://www.google.com/search?hl=en&lr=&safe=off&q=PHP+Code+Display+random+link&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
 
I

Ivo

"Geir Andersen" wrote
"Papa Legba" wrote
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/Scripts_and_Programs/Randomizing/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
 
P

Papa Legba

Ivo said:
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.
 
I

Ivo

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 :))
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top