CS1113 Web Programming
Lecture 16
Event Handling & Mathematical
Methods
Today’s Goal:
Event Handlers
• To become able to appreciate the concept of
event handlers:
– What are they?
– What do they do?
What is Event Handling?
• Capturing events and responding to them
• The system sends events to the program and
the program responds to them as they arrive
• Events can include things a user does - like
Event Driven Programs
• Programs that can capture and respond to
events are called ‘event-driven programs’
• JavaScript was specifically designed for writing
such programs
JavaScript Handling of Events
• Events handlers are placed in the BODY part of
a Web page as attributes in HTML tags
• Events can be captured and responded to
directly with JavaScript one-liners embedded in
HTML tags in the BODY portion
• Alternatively, events can be captured in the
<INPUT
type=“submit”
name=“sendEmail” value=“Send eMail”
onMouseOver=
“if (document.sendEmail.sender.value == “”)
window.alert(‘Empty From field! Please correct’)”
That was event handling through
what we may call
‘in-line JavaScript’
In-Line JavaScript Event Handling (1)
• Event handlers are placed in the BODY portion
of a Web page as attributes of HTML tags
• The event handler attribute consists of 3 parts:
1. The identifier of the event handler 2. The equal sign
In-Line JavaScript Event Handling (2)
• Multiple JavaScript statements (separated by
semicolons) can be placed in that string, but all
have to fit in a single line; no newline
characters are allowed in that string
• Due to this limitation, sophisticated event
handling is not possible with in-line event
onMouseOver=“checkForm( )”
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
JavaScript included as an attribute of the “Send eMail” button:
function checkForm() {
if ( document.sendEmail.sender.value == “”) {
window.alert( “Empty From field! Please correct” );
Usage Guideline
• For very short scripts, “all code in the tag”
works well
• The “code in the HEAD portion” is the right
choice for developing larger JavaScript scripts
– It makes the code easier to read
onClick=“uolWindow()”
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
JavaScript included as an attribute of the “New Window” button:
function uolWindow() {
A Few of My Favorite Event Handlers
onClick
onDblClick
onMouseOver onMouseDown
onFocus onBlur
There are many more: there is an
expanded, but still incomplete
list in
your book
onFocus & onBlur
• onFocus executes the specified JavaScript
code when a window receives focus or when a
form element receives input focus
• onBlur executes the specified JavaScript code
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
JavaScript included as an attribute of the INPUT tag:
function checkAge( ) {
if( parseInt( document.form1.age.value ) < 12 ) {
window.alert( "Stop! You are younger than 12" ) ; }
<HTML><HEAD>
<TITLE>onBlur( ) Demo</TITLE>
<SCRIPT>
function checkAge() {
if( parseInt(document.form1.age.value) < 12) {
window.alert("Stop! You are younger than 12" ) ; }
}
</SCRIPT>
</HEAD>
<BODY bgcolor="#66FFCC">
<FORM name="form1" method="post" action=""> <TABLE border="1">
<TR> <TD>Age</TD>
<TD><INPUT type="text" name="age" onBlur="checkAge()"> </TD></TR><TR> <TD>Phone Number</TD>
<TD><INPUT type="text" name="phNo"></TD>
onLoad & onUnload
• onLoad executes the specified JavaScript code
when a new document is loaded into a window
• onUnload executes the specified JavaScript
code when a user exits a document
• The onUnload attribute fires once a page has
unloaded (or the browser window has been closed).
• onUnload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser
window, etc.)
• <!DOCTYPE html> • <HTML>
• <TITLE>Example of onLoad Event Handler</TITLE> • <HEAD>
• <SCRIPT type=“text/javascript">
• function hello(){
• window.alert("Hello there...\nThis is an example of onLoad.");
• }
• </SCRIPT>
• </HEAD>
• <BODY onLoad="hello()">
<HTML> <HEAD>
<TITLE>onLoad Demo</TITLE> <SCRIPT type=“text/JavaScript">
var seconds = 0;
// called when the page loads to begin the timer function startTimer() {
// 1000 milliseconds = 1 second
window.setInterval("updateTime()", 1000); }
// called every 1000 ms to update the timer function updateTime() {
++seconds;
document.getElementById("time").innerHTML = seconds; }
</SCRIPT> </HEAD>
More Uses for onLoad/onUnload?
• onLoad can be used to open multiple Windows
when a particular document is opened
• onUnload can be used to say “Thank you for
the visit” when a user is leaving a Web page
• At times, a user opens multiple inter-dependent
Enhancing Forms with JavaScript
• We do care about several things.
– First values of individual form elements.
– Event cause by those form element.
– Event of entire form itself particular submitting the form.
• You have given form and field inside it
their own ids then you can ofcourse use
• Or if your HTML form have either the name
property, you can use following format too:
Text Fields
• Main properties
myTextField.value
• Main Events
onfocus (the element trigger onfocus when go into it)
onblur (trigger when you leave it, onblur is always call when you leave the field)
CheckBoxes and Radio Buttons
• Main properties
myCheckBox.checked (true or false)
• Main Events
Form Events
A Note on Syntax (1)
• Mixed-case capitalization of event handlers
(e.g. onClick) is a convention (but not a
A Note on Syntax (2)
• At times, you may wish to use event handlers in
JavaScript code enclosed in <SCRIPT>,
</SCRIPT> tags
• In those cases you have to strictly follow the
JavaScript rule for all event handler identifiers: they must all be typed in small case, e.g.
A misleading statement from Lecture 11
• I stated:
JavaScript is case sensitive. Only the first of the
following will result in the desired function – the rest will generate errors or other undesirable events:
– onMouseClick – OnMouseClick
– onmouseclick – ONMOUSECLICK
MATH Object
• Math.PI
– A property that gave us the value of Pi
• Math.round( )
– A method that rounded a number to its nearest integer
• Math.sin( )
Mathematical Functions in JavaScript
• In addition to the simple arithmetic operations
(e.g. +, *, etc.) JavaScript supports several
advanced mathematical operations as well
• Notationaly, these functions are accessed by
referring to various methods of the Math object
Methods
sin( r ) cos( r ) tan( r ) asin( x ) acos( x ) atan( x )
atan2( x, y ) max( x, y ) min( x, y ) round( x ) floor( x ) ceil( x ) exp( x )
log( x ) abs( x ) sqrt( x )
sin(
r
)
,
cos(
r
)
,
tan(
r
)
Standard trigonometric functions
Returns the sine, cosine or tangent of ‘r’, where ‘r’ is specified in radians
EXAMPLE
asin(
x
)
,
acos(
x
)
,
atan(
x
)
Standard inverse-trigonometric functions
Returns the arcsine, arccosine or arctangent of ‘r’ in radians
EXAMPLE
Returns the square root of x
sqrt(
x
)
0.5 0.7071
Returns x
raised to the power y
pow(
x, y
)
2, 32
Returns
Math.E raised to the power x
exp(
x
)
1 2.718281
Returns the the natural logarithm of x
log(
x
)
ceil(
x
)
Returns integer
nearest to x
Returns
largest integer that is less
than or equal to x
Returns smallest
integer that is greater than or equal to x
floor(
x
)
round(
x
)
1.1 1
12.5 13
1.1 1
12.5 12
1.1 2
Returns the
absolute value of x
abs(
x
)
random( )
Returns a randomly-selected, floating-point number between 0 and 1
EXAMPLE
document.write( Math.random( ) )
During Today’s Lecture …
• We looked at the concept of event-driven
programs and event handlers
– What are they?
– What do they do?
– How do we benefit from them?
• We wrote simple programs to demonstrate the