|
|
The Essence of JavaScript, Part OneBy Elizabeth Holroyd, Web Design Consultant.
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: 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: 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 ComponentsJavaScript 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.) The language also provides extensions to HTML capabilities, including pop-up alert messages, confirmation dialogue messages, and prompt dialogue boxes. The JavaScript FunctionWhen 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: |