views:

1675

answers:

6

I'm thinking that the reason I can't do this is because it might be a huge security hole, but here goes...

I want to have a bookmark on my browser (FF3, preferably) that is only a snippet of javascript code. It would merely construct a URL based on the current date and then do a window.location = on that URL.

I know that I could make a page and keep it on my local machine and just refer to it that way, but I was just wondering if you could bypass that step and actually have the "location" of the bookmark really just be javascript. I could have sworn that this was possible years ago, but I can't find anything that tells me either way now.

+23  A: 

What you want is a bookmarklet they are easy to create and should work in most major browsers.

Edit: Stack overflow seems not to allow creating bookmarklets in the context of the site, basically you can create a new bookmark and type the following in the location field

javascript:window.location='http://www.google.com/search?q='+Date()

to get a bookmarklet that searches google for the current date.

Pat
+4  A: 

What you want is a bookmarklet.

EDIT: @Pat: Jinx! You owe me a coke!

Mike Stone
+6  A: 

Well, I just created a bookmark in FF3, went back and updated it and added the following test:

javascript:alert('Wacky%20test%20yo');

Low and behold, after I saved and loaded, I was able to get my alert.

I'm sure you can work up something similar for your needs.

Dillie-O
A: 

Thanks everyone. Everything I searched for (obviously not bookmarklet) led me to thousands of scripts to create a bookmark for a site in javascript -- NOT what I was looking for.

Works like a charm now. Thank you again.

Matt Dawdy
+5  A: 

One minor catch. IE can only handle a 508 character URL in this format. If you save it in IE with a url longer than this, it will truncate without warning and thus fail.

If you need a really complex script, you'll need to use a "hosted" bookmarklet, where you have a short bookmark that injects a script tag into the page, to "call" your hosted bookmarklet.

It isn't as nice/portable, but its the only workaround.

scunliffe
So the pattern for this is something like:javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://www.yoursite.com/js/bookmarklet.js';})();
Shermozle
correct. You just need to load the script content you want from some other site.
scunliffe
A: 

Google Bookmark

javascript:(function(){var%20a=window,b=document,c=encodeURIComponent,d=a.open("http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk="+c(b.location)+"&title="+c(b.title),"bkmk_popup","left="+((a.screenX||a.screenLeft)+10)+",top="+((a.screenY||a.screenTop)+10)+",height=420px,width=550px,resizable=1,alwaysRaised=1");a.setTimeout(function(){d.focus()},300)})();