Chapter 35

JavaScript Reference


CONTENTS


This chapter serves as a tutorial and reference guide for the JavaScript language. You'll learn about the objects and methods provided by JavaScript and learn how events are handled. By the end of this chapter, you should have an understanding of what JavaScript offers and be able to begin writing your own scripts.

JavaScript Objects

JavaScript is an object-based scripting language that does not support classes or inheritance. So, you might ask, how do you use JavaScript as an object-oriented language? Good question. JavaScript provides a number of default objects to which you have direct access, and allows you to create your own objects as well. The following are the JavaScript-supplied objects:

Properties and Methods

The power of JavaScript lies in the functionality provided to the programmer. The following sections are a reference to the methods and properties of the JavaScript objects. Keep in mind when programming JavaScript that it is a loosely typed language.

The navigator Object

The navigator object provides a base class for all HTML objects. It provides the basis for the logical hierarchy inherent in HTML. That is, the navigator object contains a window object containing the document object, which in turn contains all of the defined objects.

The window Object

The window object is the base object for the document object. It provides the following properties:

The window object provides the following methods:

The location Object

The location object provides information about the current URL location, as well as some useful methods. Its properties are the following:

The location object provides the following methods:

The history Object

The history object provides access to the browser's URL history list. Its properties are the following:

The history object provides the following methods:

The document Object

The document object contains information about all HTML properties inside the current page. In addition to the standard properties provided, a member for each form in the document is provided with the name specified in the form's NAME attribute.

The following are the document object's provided properties:

The document object provides the following methods:

The form Object

The form object contains information about the defined form. In addition to its standard properties, this object contains a member for each field of the form. For instance, suppose a form contains a field called "Name"; the associated form would contain a member named Name. The value of the field can be obtained using form.Name.value.

The form object's default properties are the following:

The form object has no methods.

The button Object

The button object may take one of three separate functions coinciding with a form's reset button, submit button, or a custom button. The button object's properties are as follows:

The button object contains no methods.

The checkbox Object

The checkbox object corresponds to a form's checkbox. Its properties are as follows:

The checkbox object contains no methods.

The text, textarea, hidden, and password Objects

The text, textarea, hidden, and password objects are all formed from the same object structure. The following are the properties of the shared object structure:

The shared object structure contains these methods:

The radio Object

The radio object corresponds to a form's radio button. The object's properties are as follows:

The radio object contains no methods.

The select Object

The select object is an array corresponding to a form's selection list. Each element of the array contains the following properties:

The select object contains no methods.

The string Object

The string object provides the capability to store string data and provides methods to manipulate the string value. length identifies the length of the string.

string offers the following methods:

The Date Object

The Date object provides access to the system date function and also is a container for date information.

The Date() function creates a Date object with the current system time.

The following examples create a Date object with the specified data:

Date("month day, year hours:minutes:seconds")

Date(year, month, day)
Date(year, month, day, hour)

The Date object has no properties.

The following are the methods for the Date object:

The Math Object

The Math object provides defined math constants and some math functions. The following static constants are available:

The Math object provides the following mathematical functions:

Handling Events

Handling events in JavaScript allows the script to be informed when an event has occurred. In Java, an event is trapped and then dispatched to the code that was intended to handle that event. In JavaScript, when an object is defined in the HTML, the function used to handle that event is specified with the object. For example, look at the example of placing a button into a document:

<INPUT TYPE="button" VALUE="OK" NAME="OKButton"
onClick="ValidateMe(this.form)">

The top line defines a button with a VALUE of "OK" and a NAME of "OKButton". The bottom line is what you want to understand here.

Notice the onClick parameter to the button. This line is associating the defined function ValidateMe with the onClick event of the button. This specifies to the code to handle the event directly from HTML.

JavaScript recognizes different event handlers for a number of supplied objects. The following are all the supported event handlers with their associated objects:

For all of these events, the syntax to define the event handler is the same. Inside the object's associated HTML tag is the event's name followed by the function you want to handle the event. For example, the following:

<BODY onLoad="sayHello("Hello To Me")>

sets the function sayHello to the onLoad event of the window object.

Arrays

The syntax to declare arrays in JavaScript is very different from Java, primarily due to the loose typing of JavaScript. Arrays are declared without specifying an implicit type; JavaScript resolves the type at a later time. For example, to declare a single-dimensional array, the syntax would be as follows:

var aBunchOfInts = new makearray(50);

The syntax to load the array would be like this:

ABunchofInts[0] = 1;

Due to the dynamic binding property of JavaScript, the type of the array is not set until a value is assigned.

Operators and Expressions

JavaScript follows the Java framework for operators and expressions. Binary operators are of the form operand1 operator operand2. Unary operators are of the form operator operand1 or operand1 operator. Bitwise operators convert operands into 32-bit integers before performing the operation.

The following are all valid JavaScript operators:

+
addition
-
subtraction
*
multiplication
/
division
%
modulus
++
increment
--
decrement
&&
and
||
or
!
not
==
equal to
>
greater than
<
less than
>=
greater than or equal to
<=
less than or equal to
!=
not equal to
&
bitwise AND
|
bitwise OR
^
bitwise XOR
>>
sign-propagating right shift
<<
left shift
>>>
zero fill right fill

JavaScript supports assignment expressions using = , evaluation expressions, and conditional expressions. Conditional expressions take the form (condition) ? val1 : val2, where val1 is processed if the condition is true; otherwise val2 is processed.

Statements

JavaScript provides conditional, loop, object-manipulation, and comment statements. The syntax for each of the statement types is identical to Java. The syntaxes for the JavaScript- supported statements are as follows:

Conditional Statements

Loop Statements

Object-Manipulation Statements

Comment Statements

// comment text
/* multiple-line comment text */

Summary

The JavaScript language provides enough of a diversity from Java to retain its own identity. A large number of the statements, expressions, and operators are identical. However, the loose typing of JavaScript gives it a flavor all its own.