Lecture 7 Javascript
Lecture 7 Javascript
Handling
Function
2
Advantages of Functions
• Number of lines of code is reduced
3
Pair of
Keyword
parenthesis
Function
Function Function ‘arguments’ definition
identifier separated by commas enclosed
in a pair
of curly
function writeList( heading, words ) { braces
5
To ensure that a function is defined
before it is called up, define all
functions in the HEAD portion of Web
pages
6
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 ) ; 7
Methods
8
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
9
Predefined, Top-Level or Built-In Functions
11
Local –vs- Global
• Global variables can make the logic of a Web
page difficult to understand
HEURISTIC:
If it’s possible to
define a variable
as local, do it! 12
Event Handlers
• Special-purpose functions that come predefined
with JavaScript
13
Event Handlers
16
JavaScript Handling of Events
• Events handlers are placed in the BODY part of
a Web page as attributes in HTML tags
20
In-Line JavaScript Event Handling (1)
• Event handlers are placed in the BODY portion
of a Web page as attributes of HTML tags
21
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
22
Another - more sophisticated - way of
accomplishing the same task
23
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( )”
24
Usage Guideline
• For very short scripts, “all code in the tag”
works well
25
26
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
function vuWindow() {
window.open(“http://www.vu.edu/”) ;
}
onClick=“vuWindow()”
27
A Few of Event Handlers
onClick
onDblClick
onMouseOver
onMouseDown
onFocus onBlur
onReset
onSubmit
onLoad
onUnload
28
There are many more: there is an
expanded, but still incomplete list in
your book
29
onFocus & onBlur
• onFocus executes the specified JavaScript
code when a window receives focus or when a
form element receives input focus
30
31
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" ) ;
}
}
35
<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> 36
<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> 37
More Uses for onLoad/onUnload?
• onLoad can be used to open multiple Windows
when a particular document is opened
39
A Note on Syntax (2)
• At times, you may wish to use event handlers in
JavaScript code enclosed in <SCRIPT>,
</SCRIPT> tags
40
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
42