JAVASCRIPT

The first thing to understand about JavaScript is this: JavaScript has almost nothing to do with Java. Java is a full-fledged programming language, like C or C++, that was developed by Sun Microsystems. Java applets are small programs written in Java that are embedded in an HTML page, (usually) visible to the user the same way images or Shockwave pieces are.

JavaScript, on the other hand, is a scripting language that was developed by Netscape Communications as LiveScript; the change in name to JavaScript was a marketing move on behalf of Netscape, who got permission from Sun to use the name "Java." There are similarities between Java and JavaScript since they are both derived from C and C++ and are both object-oriented languages, and because each is designed to be able to communicate with the other. But they're essentially separate and unrelated technologies.

So if JavaScript isn't Java, what is it? It's probably easiest to think of JavaScript as an add-on to HTML. HTML is a markup language, which means that basically what it knows how to do is tell the browser how to lay out a page and what new page to load if a user clicks on a link. JavaScript gives web pages the ability to think -- to add, subtract, multiply and divide, keep track of variables, store small databases of information, talk to Java applets and (some) plugins, extract information about what platform and browser the user is using, get input from the user and process it without talking to the web server and CGI scripts, and so on. It also gives the developer more control over the look of the browser: its height and width, which information is displayed in the status bar, whether or not it has a location bar, menu buttons, scroll bars, etc.

In some ways, JavaScript scripts are like CGI scripts, except they run in the web browser instead of on the web server machine. Almost anything that CGI can do, JavaScript can do faster, as long as it doesn't require communication with the machine on which the server is running (or any other machine for that matter). JavaScript is certainly not a replacement for CGI, which is essential for tasks like processing forms, talking to databases, performing secure (or insecure) transactions, and so on. (A version of JavaScript is also designed to run, CGI-like, on some web servers -- as opposed to running in the user's browser -- but if you hear the word "JavaScript," 99.9 percent of the time it will refer to the client-side version.)

JavaScript code is written right into the HTML document, most often between <SCRIPT> tags at the top of the page, and in the body of the page in the form of event handlers. Event handlers are special bits of code that the developer can add into regular HTML tags. For example, a standard link tag looks like this:

<A HREF="hello.html">click here</A>
All this means is: When the user clicks the words "click here," load the document "hello.html." But JavaScript lets us specify instructions for other "events" that can happen to a link, such as when the user rolls his/her mouse over it. In addition to telling the browser that "click here" is a link to "hello.html," the following tag tells the browser that it should display the words "have a nice day" in its status bar when the user's mouse is over "click here":
<A HREF="hello.html" onMouseOver="window.status='have a nice day'">click here</A>
Event handlers apply mostly to links and form elements; some of them are: onClick, onMouseOver, onMouseOut, onSubmit, onSelect, onFocus, onBlur, and onChange.

JavaScript does not give the developer any more control over the layout of a page than regular HTML does.

JavaScript currently (April 1997) runs in three browsers: Netscape 2 and 3, and Internet Explorer 3 (for Windows only). JavaScript 1.0 was introduced by Netscape in Navigator 2.0. Netscape significantly enhanced the language's capabilities with Navigator 3.0, in the form of JavaScript 1.1. Microsoft introduced their version of the language, cleverly called JScript, in Internet Explorer 3.0. The versions of JavaScript that run under IE3.0 and NS2.0 are more or less the same.


So, on to some specifics. Following are quick demonstrations of some of JavaScript's capabilities. Unless otherwise specified, these features work in all three JavaScript-capable browsers (Netscape 2 and 3 and Explorer 3). It's also likely that Explorer 4 will have all or most of the JavaScript functionality of Netscape 3.

Mini-database
Most databases are huge and completely unsuited to implementation in JavaScript. But JavaScript is ideal if the contents of the database are small enough to be downloaded in one HTML page. For example, in the new Avalanche site, users will be able to view lists of our clients and projects based on the type of business the client is engaged in, the type of site, and the technologies used in the site. Handling all database searching on the client side means that the user gets results to queries instantaneously, since no cantact with the web server is necessary. Demo.

Rollovers and dynamic images (Netscape 3 only)
JavaScript can cause images to change dynamically on the page, usually when the user rolls over or clicks a link or image. The pull-down menus on the MediaOne homepage work this way. Demo.

Form validation
JavaScript can check the contents of a form to make sure that all user input is in the proper form to be processed by CGI. Such scripts can check that users have filled in all necessary fields, that someone hasn't typed their ZIP code as "100w3", that they haven't made two mutually exclusive pull-down selections, etc. All (time- and bandwidth-consuming) work that the web server would otherwise have to do. Demo.

Opening new windows
JavaScript can open a document in a new browser window, and control several of the window's attributes: its height and width, whether or not it's resizeable (Windows only), whether it has scrollbars, a status bar, menus (Windows only), a location bar, or directory buttons. JavaScript cannot, however, control the location of the new window on the screen, no matter what you may have heard. JavaScript also does not have the ability to change attributes of existing windows (such as the one the user opened when starting Netscape), only new windows. Using JavaScript to open new windows isn't always necessary, though; any frames-capable browser can open a new window without JavaScript, but the new window will have exactly the same attributes as the oniginal one. Demo.

Automatic scrolling (Netscape 3 only)
No one has caught on to the fact that JavaScript can be used to make pages scroll automatically (as if the user were slowly moving a scrollbar). This feature is of limited use, since it relies on x and y coordinates for the browser to know where to scroll to, and unpredictable font sizes and browser widths sometimes make this impossible. Demo.

Writing documents on the fly
It's unnecessary to request a new page from the web server if JavaScript has stored in memory all information it needs to create the new page. You saw this in the "mini database" above, in which all of those frames were generated by JavaScript instead of being requested from the web server. Here's another example: Demo.

Checking for plugins
JavaScript can check to see if a user has a given plugin installed. If the user doesn't have the plugin, he/she is automatically served a page where he/she can download the necessary file. Otherwise he/she is served the page that requires the given plugin. Demo.

Loading two frames at once
Something that should be built into frames-capable browsers but isn't is the ability to click on a link and have the contents of two (or more) frames change at once. JavaScript is the only way to make this happen. Demo.

Alert, confirm, and prompt dialog boxes
JavaScript allows three very basic forms of dialog box for communication with the user: the alert box (a message and an "OK" button); the confirm box (a message, an "OK" button, and a "Cancel" button -- or "Yes" and "No" buttons on some platforms), and the prompt box (a message, a text field for user entry, and "OK" and "Cancel" buttons). These are useful for quick user interaction, but more complicated interactivity (radio buttons, pull-down menus, etc.) is for now only possible with forms. Another drawback: The words "JavaScript Alert:" always appear in the box before the message as a security measure. Demo.

Browser recognition
It's often useful to know what browser, browser version, and OS the user is running. For example, dynamic image rollovers are only possible with Netscape 3, so users of other JavaScript-capable browsers can be directed to an alternate page, the navigation of which doesn't rely on rollover capability. Of course, JavaScript can only perform platform/browser recognition on JavaScript-capable browsers. (There's no demo for this, but browser recognition ensured that you were using Netscape 3 to view this presentation.)

The status bar
Browsers use their status bars to display important information to users, such as where links link to, whether a page is done loading, etc. JavaScript can seize control of the status bar from the browser, however, and in limited cases this can be useful. Usually it's just annoying to the user; many users habitually look at the status bar when they roll over a link, and may not want to see something like "Click here for great deals!!" instead of the expected URL. Demo.

JavaScript controlling RealAudio
The RealAudio plugin is supposed to be designed to accept instructions from JavaScript, but this doesn't work on all platforms. But in a roundabout way, JavaScript can still control the playing and stopping of the RealAudio plugin. Without JavaScript, the generic RealAudio "play" and "stop" buttons are the only option. Demo.

RealAudio controlling JavaScript
RealAudio files can be time-coded in such a way that they call various JavaScript functions as they play. The Warner "tout toy" makes extensive use of this.


Hope this has been helpful. Here are a few good places to explore javascript more:

JavaScript Tip of the Week
Danny Goodman's JavaScript Site
Netscape's JavaScript documentation
Microsoft's JScript documentation