HighDots Forums  

random link form current page

Javascript JavaScript language (comp.lang.javascript)


Discuss random link form current page in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Papa Legba
 
Posts: n/a

Default random link form current page - 03-03-2005 , 03:21 AM






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

Reply With Quote
  #2  
Old   
Geir Andersen
 
Posts: n/a

Default Re: random link form current page - 03-03-2005 , 08:12 AM






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" <Papa.Legba.666 (AT) gmail (DOT) com> wrote

Quote:
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...



Reply With Quote
  #3  
Old   
Ivo
 
Posts: n/a

Default Re: random link form current page - 03-03-2005 , 11:11 AM



"Geir Andersen" wrote
Quote:
"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












Reply With Quote
  #4  
Old   
Papa Legba
 
Posts: n/a

Default Re: random link form current page - 03-04-2005 , 03:31 AM



"Ivo" <no (AT) thank (DOT) you> wrote

Quote:
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.


Reply With Quote
  #5  
Old   
Ivo
 
Posts: n/a

Default Re: random link form current page - 03-04-2005 , 09:23 AM



"Papa Legba" wrote
Quote:
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








Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.