The Essence of JavaScript, Part One

By Elizabeth Holroyd, Web Design Consultant.

avaScriptTM is not JavaTM, although its syntax resembles the popular object-oriented language. Developed by Netscape®, JavaScript is a scripting language that is embedded in HTML documents and interpreted by enabled browsers, including Netscape's flagship browser, NavigatorTM, and the Microsoft® Internet ExplorerTM 3.0. Industry watchers expect Sun Microsystems, Inc., to add JavaScript capability to its HotJavaTM browser. Primary competition for Netscape's scripting language is Microsoft's VBScript, which currently runs only on Microsoft's browser.

Netscape servers that have LiveWire extensions also use JavaScripts (and VBScript can run on Microsoft's Internet Information Server version 3.0). This article introduces client-side JavaScript to people who already understand programming concepts. The discussion will continue in Part Two, which will highlight statements and more advanced JavaScripting capabilities.

Wayne Hill is MIS administrator for CitySearch, the Triangle, a website that provides event listings and features for the Raleigh-Durham-Chapel Hill, NC, area. He uses JavaScript on the CitySearch intranet. The scripts he includes in the intranet's home page display the current date and time, provide a countdown to the Triangle bureau's first anniversary, and automatically changes a graphic showing the weekday name. He has also created an internal form that uses JavaScript to gather and analyze data, and return particular text strings stored in a small database that is also in the document. The scripts run entirely on the client and only require communication with the CitySearch server to retrieve the current day graphic.

Hill, who says he thinks JavaScript will only get more popular, says he has not noticed any appreciable increase in download time, in spite of the addition of the scripting code in the HTML documents. He likes the limited amount of client side information that the scripting language recognizes, and says this makes it safe to use. He's found free scripts on the web, but cautions that many of those require the Netscape 3.0 browser to run. (If you find a script you want to save and use, you should first make absolutely sure it is for public distribution.)

The HTML file you are reading right now includes JavaScript. If your browser is able to read JavaScript, you were greeted by a message box when you loaded this page and had to click the OK button to see this article. The message box said, in part: "To fully understand this article, you should know HTML and basic programming concepts. Familiarity with objects and properties also helps." Other scripts in this file will display information on your browser's status bar and tell you what kind of music you like.

Overall, JavaScript is a language that is:

  • object based, and hierarchical, and does not allow inheritance from other objects
  • loosely typed, so you aren't required to declare variables of a specific type
  • event driven, using event handlers such as onLoad, onClick and onMouse to specify the activity that causes activation of a script
  • multi-functional, since it can be used by either client or server
  • able to pass parameters.
  • JavaScript augments rather than replaces CGI, Java, and Perl. Web page developers find that it helps make their active documents less bandwidth-intensive because JavaScript runs locally with client resources rather than communicating with the server and depending on it for computing power. When a developer wants to require a user to complete a field in a form, or use only numbers in a field, JavaScript can verify input before the form information is shipped to the server. When a developer wants to keep certain information on a page current, such as the date the page was last modified, JavaScript can retrieve and display it.

    Developers also use JavaScript to:

  • control frame and window display
  • create dynamic text presentations
  • manipulate and control HTML tags (such as forms and links) and associated browser objects (such as frames, windows, and the status bar)
  • maintain states across web pages using cookies, encoding in URL links, or hidden form variables
  • recognize and control plug-ins and MIME types
  • carry out procedures automatically or when a particular event occurs.
  • Netscape is developing software to help code JavaScript -- the preview release for Visual JavaScript is available for download until June 30, 1997. (Netscape encourages you to download the latest preview release of Netscape Communicator before installing Visual JavaScript.) You can, however, simply type JavaScript commands into your HTML document using any text editor.

    JavaScript Components

    JavaScript commands are written in lowercase only. The language consists of objects, keywords, and operators. A brief explanation of these follows. (The remainder of the article concentrates on keywords and operators.)

  • JavaScript works with objects (such as button, frame, document, and window) and their associated properties and methods. The scripting language is object based rather than object oriented. Although it uses objects and their properties, JavaScript does not allow inheritance. It operates under an instance hierarchy, since it concerns specific instances of objects rather than object classes. Refer to objects and properties using dot notation format -- objectName.propertyName -- so, window.status refers to the statusbar. (For more information about objects, check out What is Object-Oriented Software? by Terry Montlick; for a list of JavaScript objects, as well as other JavaScript specifics, see Netscape's online JavaScript documentation.)
  • JavaScript's keywords make decisions based on the present state of the computer, initiate loops, and declare new variables and functions. (Functions are subroutines that do not automatically activate -- they must be called.) Keywords are: break, continue, else, false, for, function, if, in, int, new, null, return, this, true, var, while, and with.
  • Operators control assignment, arithmetic, bitwise conversion, logical, comparison, conditional, and string concatenation.
  • The language also provides extensions to HTML capabilities, including pop-up alert messages, confirmation dialogue messages, and prompt dialogue boxes.

    The JavaScript Function

    When a JavaScript-enabled browser loads an HTML document, it evaluates the scripting code then displays the page. When you first loaded this article, a simple segment of code at the beginning of the file caused the Alert box to display as soon as your browser finished evaluating the code but before displaying the page. Other scripts depend on a subsection of code set off in a sub-routine, called a "function," that must be triggered before it will run. In JavaScript, functions help divide tasks and provide reusable pieces of code. Function code follows this format: