1. Computing

Introduction to JavaScript Bookmarklets

By

Javascript programs are most commonly run from within web pages but that is not the only place where they can be run. There are two other places within a web browser where Javascript can be run. These two places are the address bar and the favorites or bookmarks menu.

You run Javascript in the address bar by typing javascript: followed by the Javascript command(s) that you want to run. As this needs to be typed in each time that you want to run the Javascript, the use of the address bar to run Javascripts is limited.

The running of Javascripts from the favorites or bookmarks menu is far more useful than the address bar is because you can actually store small Javascripts in the menu. Even more useful is the fact that there are many such favelet or bookmarklet scripts (as Javascripts intended to run from these menus are known) that are available for you to copy and use.

These scripts will usually be presented to you on a page that describes the script and which presents you with a link that when selected will run the script immediately against the current web page. To store the script for later use you must right click on this link and select the option from the context menu to save it to your favorites or bookmarks. Depending on which browser you are using you may then get a warning message that the script is potentially unsafe. Assuming that you trust the web site where you found the script you need to tell your browser to go ahead and save the script anyway.

Once you have one or more of these scripts attached to your favorites or bookmarks menu, you can then run the script against any web page you want by simply selecting that option from the menu. Provided that the web page does not contain code that interferes with the operation of your script it will perform exactly the same regardless of which web page you run it against but any references within the script will refer to the currently displayed web page and not the page where you obtained the script.

Bookmarklet scripts can perform a variety of functions but one area where they are particularly useful is when they are designed to provide information about the current web page that assists you in fixing any errors in the functioning of your page

As an example, the "view source" bookmarklet shows what the page source looks like to the browser after the Javascripts in the page have been run enabling you to see if your Javascript is creating the expected HTML tags.

  1. About.com
  2. Computing
  3. JavaScript
  4. Bookmarklets & User Scripts
  5. Introduction to JavaScript Bookmarklets

©2014 About.com. All rights reserved.