What is JavaScript?

JavaScript** allows you to create dynamic, interactive Web-based applications that run completely within a Web browser, providing some of the functionality found in server-side CGI (Common Gateway Interface) scripts (written in PERL, C, Java, or Visual Basic.) It will work as long as your system has a JavaScript-compatible browser to interpret the code, such as Internet Explorer 3.0 or higher, or Netscape Navigator 2.0 or higher. Browser versions 4.0 or higher would be strongly recommended.

JavaScript is a natural-text, computer programming language that can be placed directly into the HTML file. It is an interpreted language; which means it is executed automatically by the built-in JavaScript interpreter within the Web browser. It does not end up as an additional file to be downloaded, as does a Java applet.

Because the JavaScript interpreter is a built-in feature of the browser, JavaScript is an open language which anyone can use without purchasing a license. However, the choice and version of the browser you use determines which version of JavaScript is understood by your browser, and how successfully a JavaScript-enhanced Web page will display in your browser.

What is JavaScript made of?

JavaScript is an object-based programming language. An object is an entity (eg. a database, a record, a field) that can be treated as an individual unit or component. Relating this to Web pages, entities within the Web page are known to JavaScript as objects.

The major JavaScript objects in the context of a Web page include:

Objects have attributes (also called "properties"), which in JavaScript, are suffixed to the object following a period (eg. window.bgcolor). Attributes can be thought of as entities within the scope or domain of the object to which they belong. Most objects, in turn, are attributes of their respective "parent" object within a hierarchical structure.

For example, information entered into a text field within a form would be stored in the "value" attribute of the text field object. (In other words, the "value" attribute of a text field represents a named memory location which holds the information entered into the text field.) The text field, in turn, is an attribute of the form object. The form object is an attribute of the document object, which is an attribute of the window object.

For JavaScript to be able to access objects, they must be named. "window", "document", and "value" are examples of objects with names already defined in the language. For JavaScript to be able to access the information entered into a text field within a form, both the form and the text field also must be named (by us.)

Using JavaScript's "dot" notation, the generic, object-based reference to the value in the text field would be written as window.document.____.____.value (or simply document.____.____.value since the window object can be left off), where the blanks would be replaced with the names given to the form and text field respectively.

If the text "Sally Jones" were typed into a text field named "name" within a form named "form1", then using JavaScript's hierarchical "dot" notation for the structure of objects, the following objects are defined:

The contents of the "name" text field could have its value displayed with the simple JavaScript statement:
document.writeln("<p>" + document.form1.name.value + "</p>");
and the contents of the "name" text field could be changed with the an equally simple JavaScript statement:

document.form1.name.value = "Sam Houston";
or back to "Sally Jones" with:
document.form1.name.value = "Sally Jones";

In a similar way, the label of a button could have its value changed with the JavaScript statement:
document.form1.button2.value = "Change the contents to Sally Jones.";
or back to "Change the contents to Sam Houston." with:
document.form1.button2.value = "Change the contents to Sam Houston.";

<This is the text field.

(After the contents is written and displayed, refresh/reload the Web page to return.)

View source

In addition to attributes, objects may also have methods (tasks, or "functions") associated with them. For example, to calculate the square root of 4, the math object with the sqrt() method would be utilized, using the JavaScript dot notation: math.sqrt(4).

The JavaScript function focus() is a defined "method" of a text field object. In the example below, clicking on one button or the other changes the focus from one text field to the other. The focus is shifted to the left-hand text field with the statement:
document.form2.field1.focus()
or to the right-hand text field with:
document.form2.field2.focus()

(field1)
(field2)
View source

To process actions taken by the user, JavaScript identifies actions as "events", and provides "event handlers" which enable Web page authors to perform some action or task (via JavaScript programming) in response to the occurrence of the event. (eg. The Web page author could utilize the "onMouseOver=" event handler to initiate JavaScript instructions which would replace (swap) an image with a different image when the user passes the mouse pointer over the original image.)

Examples of events which JavaScript can respond to include:

JavaScript provides a full set of arithmetic, relational and boolean operators, and control structures (such as "if", "for", and "while") to process information and provide programming control.


References:

JavaScript - Complete Concepts and Techniques, Shelly Cashman Series, Course Technology
JavaScript Programming, ILT series, Course Technology
JavaScript Basic, ILT series, Course Technology
JavaScript Advanced, ILT series, Course Technology
JavaScript - The Definitive Guide, 3rd edition, David Flanagan, O'Reilly
Comprehensive JavaScript, Don Gosselin, (Web Warrior Series), Course Technology
JavaScript for the World Wide Web, 3rd edition, Negrino & Smith, Peachpit Press
JavaScript Bible, 4th edition, Danny Goodman, Hungry Minds, Inc.
Javascript Examples Bible : The Essential Companion to JavaScript Bible, Danny Goodman, Hungry Minds, Inc.
Introduction to Interactive Programming on the Internet using HTML & JavaScript, Craig D. Knuckles, John Wiley & Sons, Inc.

**JavaScript is the product of a joint venture between Sun Microsystems and Netscape Communications Corp. JavaScript is available in two formats; client-side JavaScript and server-side JavaScript. Server-side JavaScript is proprietary and vendor-specific. Client-side JavaScript is the format available to HTML pages and has been standardized.


Return to the JavaScript examples.