473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Jul 20 '05 #1
5 1889
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kim Scarborough | last post by:
I found this bookmarklet that will go to a random link on the currently open page: javascript:void(location.href=document.links) Could this be changed to go to a random link from the bookmarks...
2
by: Gaikokujin Kyofusho | last post by:
Hi, I am trying to find a wikipedia bookmarklet for Opera that would allow me to lookup something highlighted on a page. I could have sworn that the one i had worked until i had to reinstall opera...
1
by: Robert Oschler | last post by:
In pre-Windows XP Internet Explorer, adding a bookmarklet for a user was really simple. All I add to do was create a link that executed addFavorite(). Internet Explorer under Windows XP won't...
4
by: gwtc | last post by:
Here is a google search site bookmarklet. This lets you search a certain website using google. What I want is the same thing, but to search a certain geocities site. When you use the current...
1
by: OtisUsenet | last post by:
Hi, I have a bookmarklet that works perfectly in Firefox, IE, Konqueror, and Opera, but in Safari 2.0.3 (417.9.2) it doesn't work. I enabled debugging and I can see "TypeError - Undefined...
25
by: bweaverusenet | last post by:
Hi. I am trying to get some javascript to work in IE6, from the address line. It works in Firefox and, I believe, IE7. It appears that with some number of nested structures, the code does not...
7
by: Amir Michail | last post by:
Hi, I would like to add a submission bookmarklet to my app that takes the current selection and pastes it into the app's rich text editor while preserving formatting. It should work in at least...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.