Javascript Event Handling
Javascript Event Handling
Lecture 32
Event Handling
(Web Development Lecture 11)
1
During the last lecture we discussed
Functions & Variable Scope
We looked at functions and their use for
solving simple problems
3
Advantages of Functions
Number of lines of code is reduced
4
Pair of
Keyword
parenthesis
Function
Function Function arguments definition
identifier separated by commas enclosed
in a pair
of curly
function writeList( heading, words ) { braces
6
To ensure that a function is defined
before it is called up, define all
functions in the HEAD portion of Web
pages
7
Two Ways of Calling Functions
function popUp( message ) { A function call
appearing as a
window.alert( message ) ;
complete
} statement
popUp( Warning! ) ;
A function call
function add( a, b ) { appearing as part
of a statement.
c=a+b; Definitions of
return c ; such functions
} include a return
sum = add( 2, 4 ) ; statement
document.write( sum ) ; 8
What Would this Statement Do?
factorial( factorial ( 3 ) ) ;
recursive
use of a function
9
Methods
10
Object: A named collection of properties
A collection
All objects have the
of properties
name property: it
holds the name of
the object (collection)
name prop 8
prop 1
prop 3 prop 5
prop 2
prop 6 prop 4 prop 7
11
Predefined, Top-Level or Built-In Functions
13
Local vs- Global
Global variables can make the logic of a Web
page difficult to understand
HEURISTIC:
If its possible to
define a variable
as local, do it! 14
Event Handlers
Special-purpose functions that come
predefined with JavaScript
15
Todays Goal:
Event Handlers
18
JavaScript Handling of Events
Events handlers are placed in the BODY part of
a Web page as attributes in HTML tags
20
21
<INPUT
type=submit
name=sendEmail
value=Send eMail
onMouseOver=
if (document.sendEmail.sender.value.length < 1)
window.alert(Empty From field! Please correct)
>
23
In-Line JavaScript Event Handling (1)
Event handlers are placed in the BODY portion
of a Web page as attributes of HTML tags
24
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
25
Another - more sophisticated - way of
accomplishing the same task
26
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
function checkForm() {
if ( document.sendEmail.sender.value.length < 1) {
window.alert( Empty From field! Please correct );
}
}
onMouseOver=checkForm( )
27
Usage Guideline
For very short scripts, all code in the tag
works well
28
Another event-handling example; this
time from lecture 18
29
30
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
function vuWindow() {
window.open(http://www.vu.edu.pk/) ;
}
onClick=vuWindow()
31
A Few of My Favorite Event Handlers
onClick
onDblClick
onMouseOver
onMouseDown
onFocus onBlur
onReset
onSubmit
onLoad
onUnload
32
There are many more: there is an
expanded, but still incomplete list in
your book
33
onFocus & onBlur
onFocus executes the specified JavaScript
code when a window receives focus or when a
form element receives input focus
34
35
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
function checkAge( ) {
if( parseInt( document.form1.age.value ) < 12 ) {
window.alert( "Stop! You are younger than 12" ) ;
}
}
39
<HTML>
<HEAD>
<TITLE>onUnload Demo</TITLE>
<SCRIPT>
function annoyUser( ) {
currentUrl = window.location ;
window.alert( "You can't leave this page" ) ;
window.location = currentUrl ;
}
</SCRIPT></HEAD>
<BODY onUnload="annoyUser( )">
This page uses the onUnload event handler
</BODY></HTML> 40
<HTML>
<HEAD>
<TITLE>onUnload Demo</TITLE>
<SCRIPT>
function annoyUser( ) {
currentUrl = window.location ;
window.alert( "You can't leave this page" ) ;
window.location = currentUrl ;
}
</SCRIPT></HEAD>
<BODY onUnload="annoyUser( )">
This page uses the onUnload event handler
</BODY></HTML> 41
More Uses for onLoad/onUnload?
onLoad can be used to open multiple Windows
when a particular document is opened
43
A Note on Syntax (2)
At times, you may wish to use event handlers in
JavaScript code enclosed in <SCRIPT>,
</SCRIPT> tags
44
A misleading statement from Lecture 18
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
46
Next (the 12 ) Web Dev Lecture:
th
Mathematical Methods
Well look at JavaScripts Math object
47