JavaScript/Bookmarklets

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: The SCRIPT Tag Index Next: Lexical Structure

Bookmarklets are one line scripts stored in the URL field of a bookmark. Bookmarklets have been around for a long time so they will work in older browsers.

JavaScript URI scheme[edit]

You should be familiar with URL that start with schemes like http and ftp, e.g. http://en.wikibooks.org/. There is also the javascript scheme, which is used to start every bookmarklet.

javascript:alert("Hello, world!");

Using multiple lines of code[edit]

Since you cannot have line breaks in bookmarklets you must use a semicolon at the end of each code statement instead.

javascript:name=prompt("What is your name?");alert("Hello, "+name);

The javascript Protocol in Links[edit]

The javascript protocol can be used in links. This may be considered bad practice because it prevents access for or confuses users who have disabled JavaScript. See Best Practices.

<a href="javascript:document.bgColor='#0000FF'">blue background</a>

Examples[edit]

A large quantity of links may be found on bookmarklets.com, which show a variety of features that can be performed within Javascript.


Previous: The SCRIPT Tag Index Next: Lexical Structure