Jiving with Javascript, Part II
By Kevin M. Savetz
"Contrary to popular misconceptions, JavaScript was not meant to be a scaled-down version of Java, nor was it intended to be a replacement for CGI [server-side] scripts. Instead, JavaScript functions as an outstanding way to enhance both," he said.
JavaScript can be used to manage user input as well as to show text, play sounds, display images, or communicate with a plug-in in response to "events" such as a mouse click or entry to a Web page.
JavaScript's syntax is loosely based on the Java language, but those who have used both languages say the similarity is very loose indeed. In fact, the closest resemblance the two languages have are their names. JavaScript was originally called "Mocha" while being developed at Netscape Communications. It was later dubbed LiveScript, and when Netscape partnered with Sun to develop Java, LiveScript was renamed JavaScript.
Java vs. JavaScript
JavaScript programs are interpreted and run entirely on the client side. This means fewer hits on the server than with Java (whose applets are compiled on the server before being interpreted by the client) or CGI (which requires that the server both do the work and take the hits).
There are other differences between these two languages. In Java, applets are distinct files from HTML pages; with JavaScript, the code is integrated into the HTML using special tags. This can cause problems with some Web browsers that do not properly handle--or ignore--JavaScript code. Like Java, JavaScript is a cross-platform language that can work with any compatible browser.
Figure 1 shows an example HTML file with an embedded JavaScript. Between the <BODY> tags, the Web browser displays a form with a space to enter a number. When the user enters a number in the field, the browser starts the script, located between the <SCRIPT> tags. The script itself makes sure the user really entered a number, and displays a dialog box if the user didn't keep the number within predefined limits.
All this takes only one hit on the server to download the HTML code and the embedded script. Doing this with a CGI script would require multiple hits on the server: once for the HTML file, and again to verify the input every time the user types into the form. JavaScript allows the client to perform more significant calculations without loading down the server.
[Move on the Part III]
Reprinted from Web Developer® magazine, Vol. 2 No. 2 Spring 1996 (c) 1996 internet.com Corporation. All rights reserved.