Example 5 page display.
Enter a number in the field and then click your mouse anywhere OUTSIDE of the field. Depending on what you enter, you will be prompted to enter another number, or thanked.
Tips and Techniques
This section describes various useful scripting techniques.
Updating Pages
JavaScript in Navigator generates its results from the top of the page down. Once something has been formatted, you can't change it without reloading the page. Currently, you cannot update a particular part of a page without updating the entire page. However, you can update a "sub-window" in a frame separately.
Printing
You cannot currently print output created with JavaScript. For example, if you had the following in a page:
This is some text.
Using Quotes
Be sure to alternate double quotes with single quotes. Since event handlers in HTML must be enclosed in quotes, you must use single quotes to delimit arguments. For example
Alternatively, you can escape quotes by preceding them by a backslash (\).
Defining Functions
It is always a good idea to define all of your functions in the HEAD of your HTML page. This way, all functions will be defined before any content is displayed. Otherwise, the user might perform some action while the page is still loading that triggers an event handler and calls an undefined function, leading to an error.
Creating Arrays
An array is an ordered set of values that you reference through an array name and an index. For example, you could have an array called emp, that contains employees' names indexed by their employee number. So emp[1] would be employee number one, emp[2] employee number two, and so on.
JavaScript does not have an explicit array data type, but because of the intimate relationship between arrays and object properties (see JavaScript Object Model), it is easy to create arrays in JavaScript. You can define an array object type, as follows:
function MakeArray(n) {
this.length = n;
for (var i = 1; i <= n; i++) {
this[i] = 0 }
return this
}
}
This defines an array such that the first property, length, (with index of zero), represents the number of elements in the array. The remaining properties have an integer index of one or greater, and are initialized to zero.
You can then create an array by a call to new with the array name, specifying the number of elements it has. For example:
emp = new MakeArray(20);
This creates an array called emp with 20 elements, and initializes the elements to zero.
Populating an Array
You can populate an array by simply assigning values to its elements. For example:
emp[1] = "Casey Jones" emp[2] = "Phil Lesh" emp[3] = "August West"and so on.
You can also create arrays of objects. For example, suppose you define an object type named Employees, as follows:
function Employee(empno, name, dept) {
this.empno = empno;
this.name = name;
this.dept = dept;
}
Then the following statements define an array of these objects:
emp = new MakeArray(3) emp[1] = new Employee(1, "Casey Jones", "Engineering") emp[2] = new Employee(2, "Phil Lesh", "Music") emp[3] = new Employee(3, "August West", "Admin")
Then you can easily display the objects in this array using the show_props function (defined in the section on the JavaScript Object Model) as follows:
for (var n =1; n <= 3; n++) {
document.write(show_props(emp[n], "emp") + "
");
}


