This chapter discusses a method for putting a program script inside the HTML page so that it runs on the browser, instead of on the server. Objectives important to this chapter are:
tag. This is a paired tag, and the pair enclose the program you will write.
The opening tag requires an attribute naming the language your script
is written in, so the format will be like this:
For those who wish to hide their scripts from the user, the <script> tag can use the src attribute, to specify a file that contains the script to be run. This is efficient in the sense that if you need to make a change in a program that several HTML pages use, you need only change the external file they reference, instead of changing all the pages individually.
can be anything that is stored in memory. In relation to programming,
an object is often a piece of a larger thing, and each piece has properties
(or attributes) that can be given values. For instance, all the parts
of an HTML page being displayed can be thought of as objects inside a
window object. The document object in that window has properties
(attributes) such as the background color. This object and property are
referred to by the phrase
document.bgcolorwhich follows the general syntax of
Different kinds of objects have different kinds of properties. Objects
also have methods. A method is like a built-in program (or function)
that the object knows how to run. Methods are used with the same kind
of syntax as properties, except that the name of the method is usually
followed by a set of parentheses. For example, if we have a form on the
screen, and we wish to submit the form, the code to do so might be
form.submit()Other properties and methods are discussed on pages 600 - 601.
Another important concept in object oriented program languages is the event. An event is when something specific happens in the program or from interaction with the user. Several events are listed on page 603.
An event is defined to happen whenever a specific action takes place, and the fact that it happens is noticed by an event handler. An event such as the user placing the cursor in a field is noticed by the handler onFocus, while clicking a button is handled by onClick. The events are associated with specific objects. That is, each button would have its own Click event and onClick handler.
What happens when an event takes place is the next concept. Events are often associated with functions. A function is a sub-program that is already included with your object or that you can write as an add-on for it. Functions are used to make programs more modular, so you can use pieces of one program in another one. They also help when writing a program, in that you can concentrate on small tasks, one at a time, and write a function for each task, instead of worrying about the program as a whole.
In order to call a function, you have to use the event handler as an
attribute to a tag, an equal sign, and the name of the function you are
calling. For example, we could put an onClick handler in a button tag,
and have the handler call a function to check something in the form, as
<input type="SUBMIT" onClick="processclick()">
in other languages. To use a variable, first declare that it exists
with code like:
var variablename = some_value;Note that this program line ends with a semicolon, just as program lines do in C or C++. The line not only declares the variable named, it also gives it an initial value, or initializes it. A variable may be declared and given a value in any of the ways depicted on page 604. There we see a string as the value, a number, the value of a property, and the return value of functions used as the value being assigned.
A test condition is usually constructed with a relational operator, such as those in the chart on the bottom of page 607. These are the standard relational operators in C.
An if statement can also be modified to include an else clause. This is useful for situations that have only two possible outcomes.
Looping is repeating the same lines of code some number of times.
It is best to remember to test the logic of a loop before turning it loose on the public. A badly worded test can result in an infinite loop, which runs forever unless terminated by the user. This will not make your web pages popular.