random link form current page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Papa Legba

    random link form current page

    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...
  • Geir Andersen

    #2
    Re: random link form current page

    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


    Another tip is to google for something like: PHP Code Display random link


    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 @gmail.com> wrote in message news:39b3863a.0 503030021.1f42f 700@posting.goo gle.com...[color=blue]
    > 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...[/color]


    Comment

    • Ivo

      #3
      Re: random link form current page

      "Geir Andersen" wrote[color=blue]
      > "Papa Legba" wrote[color=green]
      > > 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.
      > >[/color]
      > 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.[/color]

      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.r andom() ) ];

      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










      Comment

      • Papa Legba

        #4
        Re: random link form current page

        "Ivo" <no@thank.you > wrote in message news:<422737f3$ 0$9747[color=blue]
        > 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.r andom() ) ];
        >
        > 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;[/color]

        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.

        Comment

        • Ivo

          #5
          Re: random link form current page

          "Papa Legba" wrote[color=blue]
          > 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.[/color]

          <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






          Comment

          Working...