HTML Tutorial.09
HTML Tutorial.09
XP
Tutorial Objectives
Learn about form validation Study the object-based nature of the JavaScript language Work with objects, properties, methods, and events of your Web page Create a program to calculate a value
XP
XP
XP
XP
XP
XP
XP
XP
XP
11
XP
JavaScript arranges objects in a Document Object Model or DOM. The DOM defines the logical structure of objects and the way an object is accessed and manipulated. The document object model can be thought of as a hierarchy moving from the most general object to the most specific.
Creating Web Pages with HTML, 3e Prepared by: C. Hueckstaedt, Tutorial 9 12
XP
frame
event
history
document
location
navigator
screen
document
anchor
form
image
link
style
tag
button
checkbox
input
radio
reset
select
submit
textarea
13
XP
DOM Hierarchy
The topmost object in the hierarchy is the window object, which contains the other objects in the list, such as the current frame, history list, and the Web page document. The Web page document contains its own set of objects, including links, anchors, and forms. Within each form are form objects, such as input boxes, radio buttons, or selection lists.
14
XP
15
XP
16
XP
Field Names
To refer to a particular field, attach the field name to the JavaScript reference for the form.
for example, in the order form to display the current date in the formdata field use the following JavaScript object reference: document.order.formdate
17
XP
Object Collections
There is another way to reference an object and that is with an object collection.
18
XP
XP
This figure lists some of the more commonly used JavaScript object collections. Not all object collections are supported by all browsers or browser versions.
All anchor elements in the document All Java applets in the document. The applet must be started before being recognized as part of the DOM All embedded objects in the document All of the elements in the form named form.
document.forms document.frames
2.0
3.0 4.0
All inline images in the document All hyperlinks in the document All plug-ins in the document
2.0 2.0
document.scripts
4.0
20
XP
21
XP
22
XP
JavaScript Properties
There are several ways of working with properties.
the value of a property can be changed store the propertys value in a variable test whether the property equals a specified value in an IfThen expression
23
XP
24
XP
JavaScript commands
XP
Changing Properties
Not all properties can be changed. Some properties are read-only, which means that you can read the property value, but cannot modify it.
26
XP
XP
XP
This figure shows three examples of property values being assigned to JavaScript variables.
29
XP
XP
31
XP
32
XP
33
XP
Managing Events
An event is a specific occurrence within the Web browser. For example:
opening up a Web page positioning the mouse pointer over a location on that page
Events are an important part of JavaScript programming, you can write scripts that run in response to the actions of the user, even after the Web page has been opened.
Creating Web Pages with HTML, 3e Prepared by: C. Hueckstaedt, Tutorial 9 34
XP
XP
Form events
36
XP
JavaScript commands users clicks the red button users clicks the blue button initial Web page users clicks the green button
Creating Web Pages with HTML, 3e Prepared by: C. Hueckstaedt, Tutorial 9 37
XP
38
XP
JavaScript Events
This figure shows JavaScript events.
39
XP
40
XP
To run a command in response to the click event, an easy way of doing this is to create a hyperlink around the object to receive the mouse click. The syntax for doing this is:
<a href=javascript:JavaScript commands>Hypertext</a>
JavaScript commands are the commands you want to run when the text link Hypertext is clicked by the user
41
XP
The following code changes the Web pages background color to red when the hypertext Change background to red is clicked.
<a href=javascript:document.bgcolor= red;> Change background to red </a>
One advantage of this technique is that you can apply it to objects that might not support the onclick event handler in all browsers or browser versions.
Creating Web Pages with HTML, 3e Prepared by: C. Hueckstaedt, Tutorial 9 42
XP
43
XP
event handler
44
XP
XP
current date
46
XP
Netscape 2.0 6.0 2.0 6.0 3.0 2.0 Netscape 2.0 2.0 2.0 Netscape 2.0 2.0 2.0
47
XP
48
XP
49
XP
the product field receives the focus of the cursor after the current date is entered in the formdate field
50
XP
51
XP
52
XP
53
XP
54
XP
55
XP
56
XP
XP
58
XP
59
XP
The first radio button has an index value of 0, the second button has an index value of 1, and so on.
the JavaScript object references for three shipping radio buttons are:
document.order.shipping[0] document.order.shipping[1] document.order.shipping[2]
Creating Web Pages with HTML, 3e Prepared by: C. Hueckstaedt, Tutorial 9 60
XP
name value Method focus() blur() click() Event Handler onfocus() onblur() onclick()
2.0 2.0 Netscape 2.0 2.0 2.0 Netscape 2.0 2.0 2.0
61
XP
62
XP
XP
64
XP
This figure lists some of the properties, methods, and event handlers of check box objects.
Property checked name value Method focus() blur() click() Event Handler onfocus() onblur() onclick() Description A Boolean value indicating whether the check box has been checked The name of the check box field The value of the check box Description Give focus to the check box Remove focus from the check box Click the check box Description Run when the check box receives the focus Run when the check box loses the focus Run when the check box is clicked IE 3.0 3.0 3.0 IE 3.0 3.0 3.0 IE 3.0 3.0 3.0 Netscape 2.0 2.0 2.0 Netscape 2.0 2.0 2.0 Netscape 2.0 2.0 2.0
65
XP
Submitting a Form
If a condition of the form is not met, the browser should refuse the form submission and indicate to the user why the form was not submitted.
66
XP
test that a product, quantity, and shipping method has been selected test that a shipping address has been entered
test whether a billing address has been entered test whether a card name and number has been entered test whether a credit card type has been selected test whether the user has entered a billing address or complete credit information test whether the entire form has been completed 67 properly
XP
68
XP
XP
70
XP
Dialog Boxes
You may want to use a dialog box to display a message to the user to indicate that the form was not properly completed.
71
XP
72
XP
73
XP
confirm(Continue Program?)
74
XP
XP
76
XP
77
XP
Resetting a Form
When designing a form, it is important to allow the user to reset the form.
78
XP
Resetting a Form
Resetting a form does not load the page. Use JavaScript to reload the page.
this has the effect of resetting all field values and rerunning the startform() function that inserts the current date. use the location object to reload a Web page
One of the methods associated with the location object is the reload()method, which reloads the current page. The syntax is simply:
location.reload();
79
XP
To control the reset event, use the onreset handler and apply it to the <form> tag.
80
XP
81
XP
Tutorial 9 Summary
Learned the object-oriented nature of the JavaScript language. Used JavaScript as a validation tool for online forms. Learned the basic concepts of form validation. Introduced to object-based programming concepts i.e. objects, properties, and methods. Learned about events, and how to run programs in response to events.
Creating Web Pages with HTML, 3e Prepared by: C. Hueckstaedt, Tutorial 9 82
XP
83