Posted By

noah on 11/09/09


javascript js template browser simple pattern web howto bookmarklet recipe

Versions (?)

Who likes this?

4 people have marked this snippet as a favorite


Bookmarklet Template: turn any snippet of JavaScript code into a Bookmarklet

 / Published in: HTML


Will originally showed me how easy it is to transform random JS snippets into bookmarklets. Once you set up the HTML, just view it in Firefox, and then drag the link to your bookmarks toolbar.

The basic form is

<a href="javascript:CODE;void 0;">NAME</a>

Where CODE is any arbitrary JavaScript code, and NAME is the name you want the bookmarklet to have once it has been added to the Firefox toolbar.

And of course don't use the javascript: protocol in your HTML pages -- although it's (only!) OK for bookmarklets.

  1. <!-- Replace alert('hi'); with your own JavaScript. -->
  3. <a href="javascript:alert('hi');void 0;">Hello</a>
  5. <!-- For code that is likely to throw errors, wrap the body of the bookmarklet in a try/catch block: -->
  7. <a href="javascript:try{doSomething()}catch(e){};void 0;">What could go wrogn?</a>

Report this snippet  

You need to login to post a comment.