|
March 11, 2000 Bookmarklet's Foundations Tips: March 2000
Yehuda Shiran, Ph.D.
|
|
JavaScript-enabled browsers support the javascript: protocol, which lets you execute statements rather than loading a new document. For example, enter the following URL in your browser:
Now enter a two-statement URL:
As you can see, it is possible to enter multiple statements after the preceding
Entering a
Simply click the following link, and an alert box will appear: Notice the alternation of quotes. Since the HREF attribute requires quotes, we must make sure the internal javascript: URL doesn't use the same type of quotes. In the preceding example, we use double quotes for the HTML attribute, and single quotes for the JavaScript statement.
The JavaScript statement used in a When taking advantage of the javascript: protocol, we must make sure the statement doesn't return any value. Therefore, we need to apply the void operator if the statement returns an actual value. Here's an example:
The void operator evaluates its expression, and returns undefined. The parentheses surrounding the expression are optional, but they are important when writing compact, one-line scripts. If you're not sure if a statement returns a value, use the void operator to stay on the safe side. When inserting several statements in a single javascript: URL, all value-returning statements require a void operator. Take a look at the following example:
Note that the entire Learn more about javascript: scriptlets in Column 35, The javascript: Protocol.
People who read this tip also read these tips: Look for similar tips by subject: |