Java Reference
In-Depth Information
When including JavaScript in an HTML file, the JavaScript code should be
enclosed in the <script> </script> tag within the <head> </head> tag. The
<script> tag has an attribute, language, which identifies the type of script lan-
guage used. Line 11 in Figure 12-7 on page 787 uses JAVASCRIPT as the value of
the language attribute, to indicate that the script enclosed by the <script>
</script> tag conforms to the syntax for JavaScript.
The JavaScript code in the HTML file can be executed at this point in the
document or it may include one or more functions defined for later use. By
defining functions used by the document within the <head> tag, they are loaded
and available before the user can do anything that would call them.
Lines 12 through 28 display a user-defined JavaScript function enclosed in
HTML comment tags to hide the JavaScript from older, incompatible browsers. A
JavaScript function is a callable section of code, much like a method. The func-
tion, validate(), in line 13 has a single parameter, myForm, indicating the form for
which the function is called. A single HTML document commonly has only one
form; however, having multiple forms in a single HTML document is valid.
Note that no data type is specified for the form parameter. Unlike Java,
JavaScript is a loosely typed language, meaning that you do not have to declare
the data type of a variable. JavaScript determines the data type from the data
itself. Although this may seem advantageous when writing code, if not done with
great care, it can lead to errors that are difficult to solve.
A function, such as validate(), can create local variables, have parameters, or
access form fields directly. When accessing form fields directly, fully qualified
names must be used, beginning with the word, document, followed by the form
name, and then the field name, as in document.form.userID.
JavaScript also has many built-in functions that can be useful. Line 15
includes an if() function to check the userID field of the form to determine if its
value is a null, or empty, string. Note that, because the form name, myForm, is
passed as a parameter to this function, the parameter, myForm, is used as an
alias for the document.form reference.
If the user ID is a null string, a built-in function, alert() , is called in line 17
to display a dialog box with a message telling the user to please enter a user ID
and an OK button. Line 18 then calls another built-in function, focus() , which
requests focus for the userID field. Having focus means that a form field can
accept user input. This places the insertion point in the userID text box. Lines 20
through 24 perform the same action for the password field. If both fields contain
values, then the submit() built-in function for the form is called in line 26,
causing the form data to be submitted to the Web server.
The following steps enter the JavaScript code used in the HTML document,
index.html.
To Enter JavaScript in an HTML Document
1. With the TextPad window open, enter lines 1 through 10 as shown in
Figure 12-7.
TextPad displays HTML statements in the coding window (Figure 12-9). A
comment section, the beginning tags for the HTML document, head section,
and title are included in lines 1 through 10.
Search WWH ::




Custom Search