Bookmarklet for random url

M

Martin

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
 
L

Lee

Martin said:
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...

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.replace("mydomain.com/images/"
+String.fromCharCode(r(),r(),r(),r())
+".jpg");
</script>
</head>
<body>
Oops, that image doesn't exist.
</body>
</html>
 
M

Martin

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

[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
 
L

Lee

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

[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?

Nothing simpler comes to mind.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
function r(){
return Math.floor(Math.random()*26)+"a".charCodeAt();
}
location.replace("mydomain.com/images/"
+String.fromCharCode(r(),r(),r(),r())
+".jpg");


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).toString(36)

do { with (Math) x = floor(random()*1679616).toString(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 += "abcdefghijklmnopqrstuvwxyz".charAt(Math.random()*26)
x = "mydomain.com/images/"+S+".jpg"
 
M

Martin

Dr John Stockton said:
This seems OK too :

S = ""
while (S.length<4)
S += "abcdefghijklmnopqrstuvwxyz".charAt(Math.random()*26)
x = "mydomain.com/images/"+S+".jpg"

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top