Working With JavaScript Objects and Events
Working With JavaScript Objects and Events
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
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
billcb
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
onload
onunload onabort onerror onmove onresize onscroll onfocus onblur onchange onselect onsubmit onreset onkeydown onkeyup onkeypress onclick ondblclick onmousedown onmouseup onmousemove onmouseover onmouseout
2.0
2.0 3.0 3.0 4.0 4.0
3.0
3.0 4.0 4.0 3.0 4.0 4.0 3.0 3.0 3.0 3.0 3.0 4.0 4.0 4.0 4.0 3.0 4.0 4.0 4.0 4.0 3.0 4.0
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
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
The startform() function relies on another JavaScript function named todaytxt(). The code for the todaytxt() function is as follows:
function todaytxt() { var Today=new Date(); return today.getMonth()+1+/+Today.getDate()+ /+Today. getFullYear(); }
Creating Web Pages with HTML, 3e Prepared by: C. Hueckstaedt, Tutorial 9 45
XP
current date
46
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
57
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
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
The this keyword is a JavaScript object name that refers to the currently selected object. Useful in situations where several different objects on the page might access the same function.
in that situation, the this keyword can pass along information about the object that initiated the function
64
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
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
URL is the address of the Web page you want to display in the browser
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