home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


JavaScript: The Definitive Guide

Previous Chapter 1
Introduction to JavaScript
Next
 

1.6 Flavors and Versions of JavaScript

So far, we've been discussing JavaScript as if there were only one language to consider. In fact, there are several different varieties or flavors of JavaScript, and within these, there are different language versions. The subsections below sort it all out.

Standalone JavaScript

JavaScript was designed as a lightweight, general purpose scripting language, suitable for use in many different contexts. As such, there is a core JavaScript language, which is embellished with the necessary objects when it is embedded in a particular context. This book discusses the core language plus all the extensions it uses when embedded in a web browser. As we'll see below, JavaScript has also been embedded in web servers. In Example 1.3, all of the JavaScript code that queries and sets the fields of an HTML form is web-browser-specific, and is not part of the standalone core language. On the other hand, the code that defines data structures and performs computations is part of the core.

The core JavaScript language was, and continues to be, developed by Netscape (despite the word "Java" in its name, JavaScript is not a product of Sun Microsystems nor of JavaSoft). Netscape's press release announcing JavaScript[6] lists 28 companies that "have endorsed JavaScript as an open standard object scripting language and intend to provide it in future products." According to this press release, JavaScript is "an open, freely licensed proposed standard available to the entire Internet community." Netscape has released a reference implementation of the core JavaScript language in the form of a standalone version known as "JSRef". The JSRef distribution contains complete C source code for the JavaScript interpreter, so that it can be embedded into other products. Both JSRef and the language specification that accompanies it are currently available only to licensees of Netscape's Open Network Environment (ONE). (Obtaining an ONE license is free.)

[6] You can find it at: http://home.netscape.com/newsref/pr/newsrelease67.html

Because JSRef was not available in time from Netscape, Microsoft was forced to develop their own version of the JavaScript interpreter. They have named their standalone version of the language "JScript" and have made it available for licensing as well. Microsoft intends to keep JScript compatible with JavaScript.

JavaScript 1.0 and 1.1

There are currently two versions of the core JavaScript language. The version that was included in Navigator 2.0 is JavaScript 1.0. The version that is in the current JSRef and in Navigator 3.0 is 1.1. When Navigator 4.0 is released, it will contain JavaScript 1.2.

There are some significant differences between these various versions of the language. For example, JavaScript 1.1 provides much better support for arrays than JavaScript 1.0 does. Similarly, JavaScript 1.1 supports something known as a "prototype object" that makes it much easier to define complex data types. JavaScript 1.2 will also add new features to the language: current expectations are that this new version will include support for string matching with regular expressions and also for a C-style switch/case statement.

When JavaScript is embedded in a web browser, the differences between versions go beyond the core language features described above, of course. For example, Navigator 3.0 defines new objects, not available in Navigator 2.0, that allow JavaScript to manipulate images and applets. It is difficult to say whether these new features are enhancements of JavaScript 1.1 over JavaScript 1.0, or whether they are simply new features of Navigator 3.0 that are not available in Navigator 2.0. Note that in this book, Navigator 3.0 is sometimes used as a synonym for JavaScript 1.1 and Navigator 2.0 as a synonym for JavaScript 1.0, although this usage is not strictly accurate.

Finally, note that the version of JavaScript implemented in Internet Explorer is not JavaScript 1.1, but does support some JavaScript 1.1 features. The differences between the Microsoft and Netscape versions of JavaScript will be noted throughout this book.

Client-Side JavaScript

When a JavaScript interpreter is embedded in a web browser, the result is client-side JavaScript. This is by far the most common "flavor" of JavaScript; when most people refer to JavaScript, they usually mean client-side JavaScript. This book documents client-side JavaScript, along with the core JavaScript language that client-side JavaScript incorporates.

As of this writing, there are only two browsers, Netscape Navigator (versions 2.0 and 3.0) and Internet Explorer (version 3.0), that support client-side JavaScript. With Netscape's release of JSRef, we may see other browsers adopt the language as well. Unfortunately for those of us who want to write portable code, there are quite a few differences between JavaScript as implemented in Netscape's Navigator and JavaScript as implemented in Microsoft's Internet Explorer. While this book attempts to document both browsers, you'll notice that it documents Navigator by default, and Internet Explorer as a special case where it differs from Navigator.

There are a couple of reasons for this bias towards Navigator. First, Netscape created JavaScript, and so their implementation must be considered the definitive one. Second, Navigator was simply there first, and most JavaScript programmers have more experience with Navigator than they do with Internet Explorer. Third, Navigator has a more fully developed implementation of JavaScript. In Internet Explorer 3.0, JavaScript is implemented basically at the JavaScript 1.0 level. In future releases, we can expect to see Navigator and Internet Explorer come much closer to each other in terms of the features they implement.

VBScript

Besides supporting JavaScript, Internet Explorer 3.0 also support another scripting language, VBScript, which is short for "Visual Basic, Scripting Edition". VBScript is not another version of JavaScript, obviously, but is worth mentioning here anyway. As we've noted, standalone JavaScript becomes client-side JavaScript when the JavaScript interpreter is integrated into a web browser and when the web browser provides objects representing browser windows, documents, forms, and so on, that JavaScript can manipulate.

The engineers at Microsoft took this idea a small step further and kept the language interpreter and browser object model separate. By doing so, they allow arbitrary scripting languages (such as JavaScript and VBScript) to be integrated with the browser and given the ability to work with browser objects. Navigator does not support, and probably never will support, VBScript, but if you are a developer already familiar with Visual Basic, and you know that your pages will only be viewed through Internet Explorer, you may choose to use VBScript instead of JavaScript.

This book does not document VBScript. It does document all the client-side objects, what Microsoft calls the "object model" that JavaScript and VBScript use, however. Thus while the chapters on the core JavaScript language won't be of interest to VBScript programmers, the rest of this book will.

Server-Side JavaScript

We've seen how the core JavaScript language has been extended for use in web browsers. Netscape has also taken the core language and extended it in an entirely different way for use in web servers. Netscape calls their server-side JavaScript product "LiveWire," not to be confused with LiveConnect, documented in Chapter 19, LiveConnect: JavaScript and Java, or with LiveScript, which was the original name for JavaScript. As this book goes to press, the current versions of LiveWire are based on JavaScript 1.0.

There are not currently any server-side JavaScript products from other vendors. Other vendors may choose to embed JavaScript in their servers, or, because compatibility on the server side is not nearly as important as it is on the client side, other vendors may prefer to use proprietary scripting languages in their server products.

Server-side JavaScript provides an alternative to CGI scripts. It goes beyond the CGI model, in fact, because server-side JavaScript is embedded directly within HTML pages and allows executable server-side scripts to be directly intermixed with web content. Whenever a document containing server-side JavaScript code is requested by the client, the server executes the script or scripts contained in the document and sends the resulting document (which may be partially static and partially dynamically generated) to the requester. Because execution speed is a very important issue on production web servers, HTML files that contain server-side JavaScript are precompiled to a binary form that may be more efficiently interpreted and sent to the requesting client.

An obvious capability of server-side JavaScript is to dynamically generate HTML to be displayed by the client. Its most powerful features, however, come from the server-side objects it has access to. The File object, for example, allows a server-side script to read and write files on the server. And the Database object allows scripts to perform SQL database queries and updates.

Besides the File and Database objects, server-side JavaScript also provides other powerful objects, including the Request and Client objects. The Request object encapsulates information about the current HTTP request that the server is processing. This object contains any query string or form values that were submitted with the request, for example. The Client object has a longer lifetime than the Request object and allows a server-side script to save state across multiple HTTP requests from the same client. Because this object provides such an easy way to save state between requests, writing programs with server-side JavaScript feels much different from writing simple CGI scripts. In fact, it makes it feasible to go beyond writing scripts and to easily create what Netscape's documentation calls "web applications."

Because LiveWire is, at least at this point, a proprietary vendor-specific server-side technology, rather than an open client-side technology, it is not documented in this book. Nevertheless, the chapters of this book that discuss the core JavaScript language will still be valuable to LiveWire programmers.


Previous Home Next
An Example: Calculating Your Taxes with JavaScript Book Index JavaScript Security

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell