Bookmarklets: Creating Bookmarklets - www.docjavascript.com | WebReference

Bookmarklets: Creating Bookmarklets - www.docjavascript.com


Creating Bookmarklets

As you already know, any hypertext link can be used in the form of a bookmark. The same rule applies to javascript: links, so you can actually create a bookmark that executes JavaScript code -- a bookmarklet. The following link displays an alert box and loads a Web page: Doc JavaScript.

If you're running Windows, right-click the link and select "Add Bookmark" (Navigator) or "Add to Favorites..." (Internet Explorer). If you're working on a Macintosh, hold the mouse button over the link until the menu appears, and create a bookmark. Due to a bug in the Macintosh version of Navigator, the bookmark's name may not show up properly. Rather than using the link's text as the name of the bookmark, the browser uses the URL for some reason. The specific bookmark can be edited by pressing Option+B (Bookmarks), selecting the desired bookmark, and then pressing Option+I (Get Info).

There are other ways to create bookmarks. In Navigator, open the Bookmarks window, and select the "New Bookmark..." option from the File menu. Then enter the desired name and URL for the bookmark. In Internet Explorer you will need to do some hacking. Locate the Favorites folder, and create a new file for the desired bookmark. We suggest that you stick to the standard "Add Bookmark" or "Add to Favorites..." option.

Executing JavaScript from a bookmark is great, but running a script from a permanent button is even better. By dragging a javascript: link to Navigator's Personal Toolbar or Internet Explorer's Links bar, you can present a JavaScript-powered button in your browser. You can add a button to Navigator's Personal Toolbar by creating a bookmark in its folder. For more information, refer to Netscape's JavaScript Guide.

Note that the Macintosh version of Navigator 4.5 features a Personal Toolbar, but previous versions do not. You should also know that the Links bar in the Macintosh version of Internet Explorer 4.0x causes some trouble, so you may find it difficult to use.

When you trigger a bookmarklet, you are actually running a script on the current page. Therefore, you cannot use a bookmarklet to access properties of a document in another window, belonging to a different site. These are simply security precautions that are typical to JavaScript.

https://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: February 1, 1999
Revised: February 1, 1999

URL: https://www.webreference.com/js/column35/creating.html