1

I've searched around but I can't find any information about this; maybe I'm not using the best search terms.

Does a JavaScript bookmarklet - i.e., a link that uses the "javascript:" pseudo-protocol that can be dragged to a browser's address bar and opened on any page - need to be a valid URL? I've heard that it does, but I'd like to find a definitive answer.

Google Reader has a "Note in Reader" bookmarklet that uses the following JavaScript code:

javascript:var%20b=document.body;var%20GR________bookmarklet_domain='http://www.google.com';if(b&&!document.xmlVersion){void(z=document.createElement('script'));void(z.src='http://www.google.com/reader/ui/link-bookmarklet.js');void(b.appendChild(z));}else{}

The encoded spaces (%20) seem to be an effort to adhere to a valid URL, but are the curly braces and semicolons, for example, valid URL characters?

If the answer is yes, is there a JavaScript function or an online utility that I can use to make my bookmarklet a valid URL in one step? JavaScript has encodeURI, but that will also encode parentheses, curly braces, etc.

Bungle
  • 19,392
  • 24
  • 79
  • 106

2 Answers2

1

The answer to your question is yes, (However, some browsers like Firefox automatically escape things like spaces) If you want to do this, a good tool is John Gruber's Javascript Bookmarklet Perl Script (yeah, the Markdown guy!)

aviraldg
  • 9,531
  • 6
  • 41
  • 56
0

I did some bookmarklets and I don't remember to encode them like this. So I guess is not necessary (but maybe needed for certain browsers, like IE)

Ionuț Staicu
  • 21,360
  • 11
  • 51
  • 58
  • Thanks, Ionut. I also noticed that non-encoded bookmarklets worked (at least in the browsers I checked, like FF/3.6 & Saf/4), but knowing how browsers make the best of bad code, I figured it was still a good idea to follow the rules, if possible. – Bungle Mar 03 '10 at 16:04