WbWizard's JS Book




Blue Line


Lesson 21: URL Scripting


Today we will be discussing the URL as a means of scripting via the "javascript:" protocol. Just as there are several "internet" protocols with various starting sequences such as http:// or ftp:/, there is a special javascript: protocol that allows you to script within an URL, creating new documents on the fly, or evaluating expressions with no return values much in the same way as the onClick event handler. The only difference between onClick and javascript: is the ability of the URL to resolve to a new page displaying the return value of the expression.

[Note-WebTv returns true for different instances than other browsers. However, you can always include the statement "return true;" in any expression to force it to return true in the javascript: protocol.]

1) No Return Value Expressions:

You can use expressions which have no return value in the javascript: protocol, in the same fashion as the onClick event handler, Examples:





2) Return Value Expressions:

When you use expressions which have a return value in the javascript: protocol, the expression is evaluated, and the results of the last line (or semi colon seperated statement) are displayed on a new page. For example, you can display the date:



Do not place return true last or the page will only display the word true. lol

[Note- WebTv is buggy here, force true as I have when your statement wont display as discussed.]

By using this technique with limited html formatting in the return value statement, you can create dynamic pages with content generated from any data available on the page or from user input.

3) Void Operator:

What if you want to use a javascript: protocol with an expression that does have a return value, but you don't want to generate a new page? You would think returning false would work, but false is a value, and any return value will be displayed, Example:



To prevent display of the return value, even when you need to explicitly state the return statement, use the void(0); operator as your last statement, Example:



A common method of using links with onClick event handlers and no specific URL is to use the javascript: protocol with only the void(0); statement, Example:



As with event handlers, it is probably best in most circumstances to call functions with the javascript: protocol, rather than expressing statements directly in the URL, Example:





Easy Pad



Blue Line
Lessons by
Jerry aka WebWizard