Chapter 20. JavaScript

I was debating whether to include JavaScript in this book or not when I had to use JavaScript on an HTML form to enable or disable fields depending on the value of another field. Doing so on a simple web page is straightforward, but I was pleasantly surprised that the dynamic object-oriented features of JavaScript allowed me to implement this functionality in a generic way as part of an XML/XSL based web application framework. The second reason I decided to cover JavaScript is the excellent reference card I stumbled upon while solving this problem. This card shows JavaScript as a small yet powerful language.

The main difference between JavaScript and the other languages in this book it its original as a scripting language for internet browsers. JavaScript (originally called "LiveScript") was created by Netscape in 1995 to allow web designers to use the newly introduced Java applets without coding Java. Although the name stresses the relationship to Java, JavaScript is a completely different language, much closer to scripting languages such as Perl and (apart from the curly braces) Python. Rather than controlling applets, it turned out to be very useful to access and manipulate the parts of HTML pages (by means of the underlying document object model, DOM).

Microsoft first tried to position Visual Basic as an alternative browser scripting language in the form of VBScript and then added its own JavaScript implementation called JScript to the Internet Explorer in 1996. Because the differences of these implementations, a standardization process was started with ECMA which lead to the first ECMAScript standard in 1997.

We will treat JavaScript in this chapter as an independent programming language without the context of a web browser.

20.1. Software and Installation

We use Netscape's JavaScript implementation in Java, called Rhino. At the time of this writing, version 1.5 release 4.1 is the most current stable release. It implements edition 3 of the ECMA standard. The implementation includes also an interpreter with an interactive shell that we can use for our experiments. The shell is started with the command

java -classpath js.jar org.mozilla.javascript.tools.shell.Main

The Java archive js.jar contains the JavaScript implementation including the org.mozilla.javascript.tools.shell.Main class which contains the interactive shell. When running the command, the intepreter greets you with a short message.

Rhino 1.5 release 4.1 2003 04 21
js>

Bibliography

Yes, David Flanagan has also written (besides his much acclaimed "Java in a Netshell") one of the standard JavaScript books ([FLANAGAN01]>).

David Flanagan, O'Reilly, 2001, 0-596-00048-0, JavaScript: The Definitive Guide, 4th Edition.

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley, 1995, 0-201-63361-2, Design Patterns: Elements of Reusable Object-Oriented Software.