We Recommend

HTML: The Definitive Guide HTML: The Definitive Guide
They teach you that learning HTML is like learning any other language and that reading a book of rules can only take you so far. Readers begin writing what may be their first Web page just two pages into the book's second chapter. From there on, they provide a wide range of HTML coding to allow readers to learn from good examples. The book includes a handy "cheat sheet" of HTML codes for quick reference.


Posted By

noah on 11/09/09


Tagged

javascript js template browser simple pattern web howto bookmarklet recipe


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

McWatt


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


Published in: HTML 


URL: http://onemorebug.com/bookmarklets

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. -->
  2.  
  3. <a href="javascript:alert('hi');void 0;">Hello</a>
  4.  
  5. <!-- For code that is likely to throw errors, wrap the body of the bookmarklet in a try/catch block: -->
  6.  
  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.