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.appe开发者_如何学编程ndChild(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.
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!)
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)
精彩评论