Bookmarklet for random url

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

    #1

    Bookmarklet for random url

    I've found examples of bookmarklets that select a URL from a page at
    random, but I'd like one that generates part of the URL at random.

    If you have a bunch of images that are named:
    mydomain.com/images/abcd.jpg where abcd is can be any combination of 4
    lowercase letters - is there a way of having the bookmarklet generate
    the letters randomly when clicked?

    I'm able to do it using AppleScript & Safari but would like it to work
    from within Safari - which is missing the "Scripts" menu that most Apple
    apps have :(

    It looks as if bookmarklets are the only option, but I've only ever used
    javascript to select randomly from existing arrays of images and don't
    fancy typing in the names of a couple of thousand images...

    TIA

    --
    Martin
  • Lee

    #2
    Re: Bookmarklet for random url

    Martin said:[color=blue]
    >
    >I've found examples of bookmarklets that select a URL from a page at
    >random, but I'd like one that generates part of the URL at random.
    >
    >If you have a bunch of images that are named:
    >mydomain.com/images/abcd.jpg where abcd is can be any combination of 4
    >lowercase letters - is there a way of having the bookmarklet generate
    >the letters randomly when clicked?
    >
    >I'm able to do it using AppleScript & Safari but would like it to work
    >from within Safari - which is missing the "Scripts" menu that most Apple
    >apps have :(
    >
    >It looks as if bookmarklets are the only option, but I've only ever used
    >javascript to select randomly from existing arrays of images and don't
    >fancy typing in the names of a couple of thousand images...[/color]

    The easy solution is to create a regular bookmark to a page containing:

    <html>
    <head>
    <script type="text/javascript">
    function r(){
    return Math.floor(Math .random()*26)+" a".charCodeAt() ;
    }
    location.replac e("mydomain.c om/images/"
    +String.fromCha rCode(r(),r(),r (),r())
    +".jpg");
    </script>
    </head>
    <body>
    Oops, that image doesn't exist.
    </body>
    </html>

    Comment

    • Martin

      #3
      Re: Bookmarklet for random url

      In article <br2cbt01jop@dr n.newsguy.com>,
      Lee <REM0VElbspamtr ap@cox.net> wrote:
      [color=blue]
      > The easy solution is to create a regular bookmark to a page containing:[/color]

      [snip]

      Thanks for that Lee - funny how you get stuck thinking that the answer
      lies in one direction (bookmarks) when there is a much simpler solution.

      One more question :-)

      If I wanted to include numbers as well as letters, do I need to state a
      string first (0-9, a-z etc) and get the script to select from items 1-36
      of the string or is there a shortcut?

      Regards

      --
      Martin

      Comment

      • Lee

        #4
        Re: Bookmarklet for random url

        Martin said:[color=blue]
        >
        >In article <br2cbt01jop@dr n.newsguy.com>,
        > Lee <REM0VElbspamtr ap@cox.net> wrote:
        >[color=green]
        >> The easy solution is to create a regular bookmark to a page containing:[/color]
        >
        >[snip]
        >
        >Thanks for that Lee - funny how you get stuck thinking that the answer
        >lies in one direction (bookmarks) when there is a much simpler solution.
        >
        >One more question :-)
        >
        >If I wanted to include numbers as well as letters, do I need to state a
        >string first (0-9, a-z etc) and get the script to select from items 1-36
        >of the string or is there a shortcut?[/color]

        Nothing simpler comes to mind.

        Comment

        • Dr John Stockton

          #5
          Re: Bookmarklet for random url

          JRS: In article <br2cbt01jop@dr n.newsguy.com>, seen in
          news:comp.lang. javascript, Lee <REM0VElbspamtr ap@cox.net> posted at Mon,
          8 Dec 2003 09:28:29 :-
          [color=blue]
          > function r(){
          > return Math.floor(Math .random()*26)+" a".charCodeAt() ;
          > }
          > location.replac e("mydomain.c om/images/"
          > +String.fromCha rCode(r(),r(),r (),r())
          > +".jpg");[/color]


          For amusement : the first generates a string of up to 4 alphanumerics,
          and the second generates four-letter words. 1679616 = 36^4

          with (Math) floor(random()* 1679616).toStri ng(36)

          do { with (Math) x = floor(random()* 1679616).toStri ng(36) }
          while (!/[a-z]{4}/.test(x))

          In the first, subtract 36^3 from 36^4 and add 36^3 to the result of
          floor, and there should always be 4 alphanumerics, with the first in
          1..z.

          This seems OK too :

          S = ""
          while (S.length<4)
          S += "abcdefghijklmn opqrstuvwxyz".c harAt(Math.rand om()*26)
          x = "mydomain.c om/images/"+S+".jpg"

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          • Martin

            #6
            Re: Bookmarklet for random url

            In article <Gput2JCEUQ1$Ew vP@merlyn.demon .co.uk>,
            Dr John Stockton <spam@merlyn.de mon.co.uk> wrote:
            [color=blue]
            > This seems OK too :
            >
            > S = ""
            > while (S.length<4)
            > S += "abcdefghijklmn opqrstuvwxyz".c harAt(Math.rand om()*26)
            > x = "mydomain.c om/images/"+S+".jpg"[/color]

            Thanks

            I used this one - as it was the only one I could actually understand :(
            - by adding 0 thru 9 to the string and multiplying by 36.

            Regards

            --
            Martin

            Comment

            Working...