By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Bookmarklet for random url

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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>

Jul 20 '05 #2

P: n/a
In article <br*********@drn.newsguy.com>,
Lee <RE**************@cox.net> wrote:
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

--
Martin
Jul 20 '05 #3

P: n/a
Lee
Martin said:

In article <br*********@drn.newsguy.com>,
Lee <RE**************@cox.net> wrote:
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.

Jul 20 '05 #4

P: n/a
JRS: In article <br*********@drn.newsguy.com>, seen in
news:comp.lang.javascript, Lee <RE**************@cox.net> posted at Mon,
8 Dec 2003 09:28:29 :-
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"

--
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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

P: n/a
In article <Gp**************@merlyn.demon.co.uk>,
Dr John Stockton <sp**@merlyn.demon.co.uk> wrote:
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

--
Martin
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.