Lecture - 4 JavaScript and HTML Docs
Lecture - 4 JavaScript and HTML Docs
1
Window Object
History Location
Document
Link Anchor
Text- Options
Radio box
Button
Check-
box
Password Submit Reset
2
A table in XHTML
<!DOCTYPE html>
<!-- table2.html
A simple table to demonstrate DOM trees
-->
<html lang = "en">
<head> <title> A simple document </title>
<meta charset = "utf-8" />
</head>
<body>
<table>
<tr>
<th> </th>
<td> Apple </td>
<td> Orange </td>
</tr>
<tr>
<th> Breakfast </th>
<td> 1 </td>
<td> 0 </td>
</tr>
</table>
</body>
</html>
3
DOM Structure of the Document
Document
<head> <body>
<title> <table>
DOM – An Example
• The following tag:
<input type = "text"
name = "address">
corresponds to an object, which has two
properties type (which is text) and name
(which is address).
4
Element Access in JavaScript
• Manipulating elements in an HTML document
requires that you have the address of the
corresponding object.
• This can be handled in several different ways,
depending on which version of DOM to which
you seek to conform.
• The address of a JavaScript object associated
with an HTML element is called its DOM
address.
<body>
<form name = "myForm" action = "">
<input type = "button" name = "turnItOn">
</form>
</body>
</html>
5
Getting the DOM Address
• We can use the forms and elements arrays:
var dom = document.forms[0].elements[0];
If another button were added before turnItOn, the address
could no longer be accessed this way.
• We can use the name given to the item (this requires that every
enclosing item (up to but not including body) must have a
name:
var dom = document.myForm.turnItOn;
The problem is that XHTML 1.1 does not allow forms to have
a name. This causes a validation problem.
getElementById
• A better way involves using getElementById:
var dom = document.getElementById("turnItOn");
6
Checkboxes
<form id = "vehicleGroup">
<input type = "checkbox name = "vehicles"
value = "car" /> Car
<input type = "checkbox name = "vehicles"
value = "truck" /> Truck
<input type = "checkbox name = "vehicles"
value = "bike" /> Bike
</form>
… …
var numChecked = 0;
var dom = document.getElementById("vehicleGroup");
for (index = 0; index < dom.vehicles.length; index++)
if (dom.vehicles[index].checked)
numChecked++;
7
Basic Concepts of Event Handling
• An event is a notification that something specific has
happened, such as when a document finishes loading,
a button is pushed or contents of a textbox is
changed.
• An event handler is a script that is implicitly executed
in response to an event happening.
• Event-driven programming is when parts of the
programming are executed in an unpredictable
sequence in response to specific events.
Events in JavaScript
8
Events, Attributes and Tags
• The same attribute can appear in several
different tags.
• An XHTML element is said to get focus when
the user puts the mouse over it and left-clicks
or tabs over to the element.
• An element gets blurred when the user move
the cursor away and left-clicks or when (s)he
tabs away.
9
Events, Attributes and Tags
Event Tag Attribute
blur onblur
change onchange
click onclick
dblclick ondblclcik
focus onfocus
keydown onkeydown
keypress onkeypress
keyup onkeyup
load onload
10
Event Attributes and Their Tags
Attribute Tag Description
onblur <a> The link loses the input focus
<button> The button loses the input focus
<input> The input element loses the input focus
<textarea> The text area loses the input focus
<select> The selection element loses the input focus
onchange <input> The input element is changed & loses
input focus
<textarea> The text area is changed & loses input
focus
<select> The selection element is changed & loses
input focus
onclick <a> The user clicks on the link
<input> The input element is clicked
11
Attribute Tag Description
onmousemove Most elements The user moves the mouse cursor within
the element
onmouseout Most elements The mouse cursor is moved away from
being over the element
onmouseover Most elements The mouse cursor is moved over the
element
onmouseup Most elements The left mouse button is unclicked
onreset <form> The reset button is clicked
onselect <input> The mouse cursor is moved over the
element
<textarea> The text area is selected within the text
area
onsubmit <form> The submit button is pressed
onunload <body> The user exits the document
12
load.html
<!DOCTYPE html>
<!-- load.html
An example to illustrate the load event
A document for load.js
-->
<html lang = "en">
<head>
<title> onLoad event handler </title>
<meta charset = "utf-8" />
<script type = "text/javascript"
src = "load.js" >
</script>
</head>
<body onload = "loadGreeting();">
<p />
</body>
</html>
load.js
// load.js
// An example to illustrate the load event
13
Handling Events from Button Elements
• Buttons provide a simple and effective way to
get input in a web document.
• The most commonly used event created by
button actions is click.
Plain Buttons
• A plain button represents a simple situation.
• Let's assume that we create the button:
<input type = "button"
name = "freeOffer"
id = "freeButton">
• We can register a handler for this button:
<input type = "button" name = "freeButton" id =
"freeButton" onclick = "freeButtonHandler();" />
• Or we can register it by writing:
document.getElementById("freeButton").onclick
= freeButtonHandler();
14
Radio Buttons
• Radio buttons handle a choice made from a set
of options.
• In the following example, the calls to the event
handlers send the value of the pressed radio
button to the handler.
radioclick.html
<!DOCTYPE html>
<!-- radioClick.html
An example of the use of the click event with
radio buttons, registering the event handler by
assignment to the button attributes
-->
<html lang = "en">
<head>
<title> Illustrate messages for radio buttons
</title>
<meta charset = "utf-8">
<script type = "text/javascript" src =
"radio_click.js">
</script>
</head>
15
<body>
<h4> Cessna single-engine airplane
descriptions </h4>
<form id = "myForm" action ="handler">
<p>
<input type = "radio" name = "planeButton"
value = "152" onclick = "planeChoice(152)" />
Model 152
<br />
<input type = "radio" name = "planeButton"
value = "172" onclick = "planeChoice(172)" />
Model 172 (Skyhawk)
<br />
</html>
16
radio_click.js
// radio_click.js
// An example of the use of the click event with
// radio buttons attributes
case 172:
alert("The smaller of two four-place "
+ " airplanes");
break;
case 182:
alert("The larger of two four-place"
+ " airplanes");
break;
case 210:
alert("A six-place high-performance"
+ " airplane");
break;
17
default:
alert("Error in JavaScript function"
+ " planeChoice");
break;
}
}
18
radioclick2.html
<!-- radioClick2.html
A document for radio_clicks2.js
An example of the use of the click event with
radio buttons,
registering the event handler by assignment to
the button
attributes
-->
<html lang = "en">
<head>
<title> Illustrate messages for radio buttons
</title>
<meta charset = "utf-8">
<script type = "text/javascript"
src = "radio_clicks2.js">
</script>
</head>
<body>
<h4> Cessna single-engine airplane
descriptions </h4>
<form id = "myForm" action = "">
<p>
<label> <input type = "radio"
name = "planeButton"
value = "152" />
Model 152 </label>
<br />
<label> <input type = "radio"
name = "planeButton"
value = "172" />
Model 172 (Skyhawk) </label>
<br />
19
<label> <input type = "radio"
name = "planeButton"
value = "182" />
Model 182 (Skylane) </label>
<br />
<label> <input type = "radio"
name = "planeButton"
value = "210" />
Model 210 (Centurian) </label>
<br />
</p>
</form>
<script type = "text/javascript"
src = "radio_clicks2r.js">
</script>
</body>
</html>
radio_clicks2.js
// radioClicks2.html
// An example of the use of the click event with
// radio buttons, registering the event handler by
// assignment to the button attributes
20
// Determine which button was pressed
for (var index = 0;
index < dom.planeButton.length;
index++) {
if (dom.planeButton[index].checked) {
plane = dom.planeButton[index].value;
break;
}
}
// Produce an alert message about the chosen
// airplane
switch(plane) {
case "152":
alert("A small two-place airplane for"
+ " flight training");
break;
case "172":
alert("The smaller of two four-place "
+ "airplanes");
break;
case "182":
alert("The larger of two"
+" four-place airplanes");
break;
case "210":
alert("A six-place high-performance "
+ " airplane");
break;
21
default:
alert("Error in JavaScript function"
+ " planeChoice");
break;
}
}
radio_clicks2r.js
//radio_clicks2r.js
// the event registering code for radio_clicks2
var dom = document.getElementById("myForm");
dom.elements[0].onclick = planeChoice;
dom.elements[1].onclick = planeChoice;
dom.elements[2].onclick = planeChoice;
dom.elements[3].onclick = planeChoice;
22
Handling Events from Text Box and
Password Elements
• Text boxes and password boxes can create four
distinct events:
– blur
– focus
– change
– select
23
nochange.html
<!DOCTYPE html>
<!-- nochange.html
This document illustates using the focus event
to prevent the user from changing a text field
-->
<body>
<form action = "">
<h3> Coffee Order Form </h3>
24
<td> <input type = "text"
id = "french"
size = "2" /> </td>
</tr>
<tr>
<th> Hazelnut Cream (1 lb.) </th>
<td> $3.95 </td>
<td> <input type = "text"
id = "hazelnut"
size = "2" /> </td>
</tr>
<tr>
<th> Columbian (1 lb.) </th>
<td> $4.59 </td>
<td> <input type = "text"
id = "columbian"
size = "2" /> </td>
</tr>
</table>
25
</form>
</body>
</html>
nochange.js
// This script illustrates using the focus event
// to prevent the user from changing a text field
26
// Compute the cost
document.getElementById("cost").value =
totalCost = french * 3.49 + hazelnut * 3.95
+ columbian * 4.59;
}
27
Handling Invalid Input
• The handler should return false to tell the
browser not to perform any default actions.
This prevents bad data from being sent to the
server.
• Checking passwords involve making sure that
the original entry is made and that the second
entry matches it. It should be called if the
second entry blurs or if the submit button is
pressed.
pswdCheck.html
<!DOCTYPE html>
<!-- pswdChk.html
An example of input password checking, using
the submit event
-->
<html lang = "en">
<head>
<title> Illustrate password checking </title>
<meta charset = "utf-8" />
<script type = "text/javascript"
src = "pswdchk.js">
</script>
</head>
28
<body>
<h3> Password Input </h3>
<form id = "myForm" action = "">
<p>
<label> Your password
<input type = "password" id = "initial"
size = "10" />
</label>
<br /> <br />
<label> Verify password
<input type = "password" id = "second"
size = "10" />
</label>
<br /> <br />
document.getElementById("second").onblur
= chkPasswords;
document.getElementById("myForm").onsubmit
= chkPasswords;
</script>
</body>
</html>
29
Checking the Forms of a Name and Telephone
Number
validator.html
<!DOCTYPE html>
<!-- validator.html
An example of input validation using the change
and submit events
-->
30
<body>
<h3> Customer Information </h3>
<form action = "">
<p>
<label>
<input type = "text" id = "custName"
onchange = "chkName();" />
Name (last name, first name,
middle initial)
</label>
<br /> <br />
<label>
<input type = "text" id = "phone" />
Phone number (ddd-ddd-dddd)
</label>
<br /> <br />
</script>
</body>
</html>
31
validator.js
// validator.js
// An example of input validation using the change
// and submit events.
if (pos !=0) {
alert("The name you entered (" + myName.value +
") is not in the correct form. \n" +
"The correct form is: " +
"last-name, first-name, middle \n" +
"Please go back and fix your name");
myName.focus();
myName.select();
return false;
}
else
return true;
}
32
// The event handler function for the phone number
// text box
function chkPhone() {
var myPhone = document.getElementById("phone");
if (pos != 0) {
alert("The phone number you enter ("
+ myPhone.value
+ ") is not in the correct form. \n"
+ "The correct form is: ddd-ddd-dddd \n"
+ "Please go back and fix your phone "
+ "number");
myPhone.focus();
myPhone.select();
return false;
}
else
return true;
}
33
validatorr.js
// validatorr.js
// Register the event handlers for validator.html
document.getElementById("custName").onchange
= chkName;
document.getElementById("phone").onchange = chkPhone ;
34
HTMLEvents and MouseEvents
Module Event Interface Event Types
Event Propagation
• An event object is create at a node in the document
tree; the node is called the target node.
• Event creation causes a three-phase process:
– Capturing phase – The even starts at the document's root
node and propagates to the target node. Any registered
handlers are checked to determine if they are enabled. Any
enabled handler is executed.
– The registered handler for the event is executed.
– Bubbling phase – as the event bubbles back up the tree, any
registered handlers are executed.
• Any handler can be stopped from further propagation
using the stopPropagation() method.
35
Event Handler Registration
• Event handler registration is handled in DOM
2 model by the AddEventListener method.
• The call:
document.custName.addEventListener("change",
chkName, false);
36
<!-- Script to define the event handlers -->
<script type = "text/javascript"
src = "validator2.js">
</script>
</head>
<body>
<h3> Customer Information </h3>
<form action = "">
<p>
<label>
<input type = "text" id = "custName" />
Name (last name, first name, middle initial)
</label>
<br /> <br />
<label>
<input type = "text" id = "phone" />
Phone number (ddd-ddd-dddd)
</label>
<br /> <br />
</body>
</html>
37
validator2.js
// validator2.js
//*************************************************//
// The event handler for the name text box
function chkName(event) {
if (pos !=0) {
alert("The name you entered (" + myName.value +
") is not in the correct form. \n" +
"The correct form is: " +
"last-name, first-name, middle \n" +
"Please go back and fix your name");
// myName.focus();
// myName.select();
}
}
38
// The event handler function for the phone number
// text box
function chkPhone(event) {
var myPhone = event.currentTarget;
if (pos != 0) {
alert("The phone number you enter ("
+ myPhone.value +
") is not in the correct form. \n"
+ "The correct form is: ddd-ddd-dddd \n"
+ "Please go back and fix your phone "
+ "number");
}
}
validator2r.js
// validator2r.js
// The last part of validator2. Registers the
// event handlers
// Note: This script does not work with IE6
39
The navigator Object
• The navigator object allows the script to
determine the viewing browser and to call the
appropriate handler for that browser
40
The canvas Element – An Example
<canvas id = "myCanvas" height = "200"
width = "400">
Your browser does not support the canvas element
</canvas>
navigate.html
<!DOCTYPE html>
<!-- navigator.html
A document for navigator.js
Call the event handler on load
-->
<html lang = "en">
<head>
<title> navigate.html </title>
<meta charset = "utf-8" />
<!-- Script to define the event handlers -->
<script type = "text/javascript“
src = "navigate.js">
</script>
</head>
<body onload = "navProperties()">
</body>
</html>
41
navigate.js
// navigate.js
// An example of using the navigator object
42