SlideShare une entreprise Scribd logo
1  sur  47
1
CS101 Introduction to Computing
Lecture 32Event Handling
(Web Development Lecture 11)
2
During the last lecture we discussed
Functions & Variable Scope
• We looked at functions and their use for
solving simple problems
• We became familiar with a couple of
JavaScript’s built-in functions
• We became familiar with the concept of local
and global variables
3
Function
A group of statements that is put together
(or defined) once and then can be used
(by reference) repeatedly on a Web page
Also known as subprogram, procedure,
subroutine
4
Advantages of Functions
• Number of lines of code is reduced
• Code becomes easier to read & understand
• Code becomes easier to maintain as changes
need to be made only at a single location
instead multiple locations
5
function writeList( heading, words ) {
document.write( heading + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
document.write( words[ k ] + "<BR>" ) ;
}
}
Keyword
Function
identifier
Pair of
parenthesis
Function ‘arguments’
separated by commas
Function
definition
enclosed
in a pair
of curly
braces
6
Arguments of a Function
• A comma-separated list of data
• Arguments define the interface between the
function and the rest of the Web page
• Arguments values are passed to the
function by value (some popular languages
pass arguments ‘by reference’ as well)
7
To ensure that a function is defined
before it is called up, define all
functions in the HEAD portion of Web
pages
8
Two Ways of Calling Functions
function add( a, b ) {
c = a + b ;
return c ;
}
sum = add( 2, 4 ) ;
document.write( sum ) ;
function popUp( message ) {
window.alert( message ) ;
}
popUp( “Warning!” ) ;
A function call
appearing as part
of a statement.
Definitions of
such functions
include a ‘return’
statement
A function call
appearing as a
complete
statement
9
What Would this Statement Do?
factorial( factorial ( 3 ) ) ;
This is termed as the
recursive
use of a function
10
Methods
• Methods are functions
• They are unusual in the sense that they
are stored as properties of objects
11
Object: A named collection of properties
prop 1
prop 2
prop 5
name
prop 3
prop 4
A collection
of properties
All objects have the
“name” property: it
holds the name of
the object (collection)
prop 7prop 6
prop 8
12
Predefined, Top-Level or Built-In Functions
• Event handlers are not the only functions that
come predefined with JavaScript. There are
many others.
• Practically, there is no difference between
predefined functions and those that are defined
by the programmer (termed as user-defined or
custom functions)
• There are many of them, but here we discuss
only two: parseInt( ), parseFloat( )
13
Local Variables
• Declaring variables (using the var
keyword) within a function, makes them
local
• They are available only within the
function and hold no meaning outside of
it
14
Local –vs- Global
• Global variables can make the logic of a Web
page difficult to understand
• Global variables also make the reuse and
maintenance of your code much more difficult
HEURISTIC:
If it’s possible to
define a variable
as local, do it!
15
Event Handlers
• Special-purpose functions that come
predefined with JavaScript
• They are unusual in the sense that they are
mostly called from the HTML part of a Web
page and not the <SCRIPT> … </SCRIPT> part
16
Today’s Goal:
Event Handlers
• To become able to appreciate the concept of
event handlers:
– What are they?
– What do they do?
– How do we benefit from them?
• To learn to write simple programs that use
event handlers
17
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
clicking the mouse - or things that the system
itself does - like updating the clock. Today we
will exclusively focus on user-events
18
Event Driven Programs
• Programs that can capture and respond to
events are called ‘event-driven programs’
• JavaScript was specifically designed for writing
such programs
• Almost all programs written in JavaScript are
event-driven
19
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
HTML code, and then directed to a JavaScript
function for an appropriate response
20
Let’s now revisit lecture 15 where we
introduced event handlers for the first
time
21
22
<INPUT
type=“submit”
name=“sendEmail”
value=“Send eMail”
onMouseOver=
“if (document.sendEmail.sender.value.length < 1)
window.alert(‘Empty From field! Please correct’)”
>
Additional JavaScript code for the smart ‘Send
eMail’ button that does not allow itself to be
clicked if the “From” text field is left blank
23
That was event handling through
what we may call ‘in-line JavaScript’
That is, the event was captured and
handled with a JavaScript one-liner
that was embedded in the HTML tag
24
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
3.A string consisting of JavaScript statements
enclosed in double or single quotes
25
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
handling
26
Another - more sophisticated - way of
accomplishing the same task
27
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.length < 1) {
window.alert( “Empty From field! Please correct” );
}
}
28
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
– It allows the reuse of a function for multiple event
handlers
29
Another event-handling example; this
time from lecture 18
30
31
onClick=“vuWindow()”
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
JavaScript included as an attribute of the “New Window” button:
function vuWindow() {
window.open(“http://www.vu.edu.pk/”) ;
}
32
A Few of My Favorite Event Handlers
onClick
onDblClick
onMouseOver
onMouseDown
onFocus onBlur
onReset
onSubmit
onLoad
onUnload
33
There are many more: there is an
expanded, but still incomplete list in
your book
Now let’s look at some of these error
handlers in a bit more detail
34
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
when a window loses focus or a form element
loses focus
35
36
<INPUT type="text" name="age"
onBlur="checkAge( ) "
>
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" ) ;
}
}
37
<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>
</TR><TR> <TD><INPUT type="reset" value="Reset"></TD>
<TD><INPUT type="submit" value="Submit"></TD></TR>
</TABLE></FORM></BODY></HTML>
38
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
• What is the key difference between these 2 and
the 4 event handlers (onMouseOver, onClick,
onFocus, onBlur) that we have used so far?
39
onUnloadDemo.htmonUnloadDemo.htmhttp://www.vu.edu.pk/onUnloadDemo.htm
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
<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>
42
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
windows of a Web site (e.g. VULMS).
onUnload can be used to warn that all child
Windows will become inoperable if the user
closes the parent Window
43
A Note on Syntax (1)
• Mixed-case capitalization of event handlers
(e.g. onClick) is a convention (but not a
requirement) for JavaScript event handlers
defined in HTML code. Using ‘ONCLICK’ or
‘onclick’ as part of a an HTML tag is perfectly
legal as well
44
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.
‘onclick’ or ‘onmouseover’
45
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
• That statement is incorrect in two ways:
1. All four will work fine as part of HTML tags
2. Only the ‘all small case’ version will be interpreted
as intended in JavaScript code
46
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
capabilities of a few event handlers
47
Next (the 12th
) Web Dev Lecture:
Mathematical Methods
• We’ll look at JavaScript’s Math object
• We will produce solutions for simple problems
using various methods of the Math object

Contenu connexe

Tendances

JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101ygv2000
 
Intro to Dependency Injection - Or bar
Intro to Dependency Injection - Or bar Intro to Dependency Injection - Or bar
Intro to Dependency Injection - Or bar DroidConTLV
 
Lab#1 - Front End Development
Lab#1 - Front End DevelopmentLab#1 - Front End Development
Lab#1 - Front End DevelopmentWalid Ashraf
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testingVikas Thange
 
JavaScript Best Pratices
JavaScript Best PraticesJavaScript Best Pratices
JavaScript Best PraticesChengHui Weng
 
Dagger 2 - Injeção de Dependência
Dagger 2 - Injeção de DependênciaDagger 2 - Injeção de Dependência
Dagger 2 - Injeção de DependênciaEdson Menegatti
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
An Introduction to JavaScript
An Introduction to JavaScriptAn Introduction to JavaScript
An Introduction to JavaScripttonyh1
 
Extending the build workflow of TFS 2010
Extending the build workflow of TFS 2010Extending the build workflow of TFS 2010
Extending the build workflow of TFS 2010Gian Maria Ricci
 
React – Structure Container Component In Meteor
 React – Structure Container Component In Meteor React – Structure Container Component In Meteor
React – Structure Container Component In MeteorDesignveloper
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJamshid Hashimi
 
Best Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIBest Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIICS
 
JavaScript Modules Done Right
JavaScript Modules Done RightJavaScript Modules Done Right
JavaScript Modules Done RightMariusz Nowak
 

Tendances (20)

JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
ReactJS
ReactJSReactJS
ReactJS
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Intro to Dependency Injection - Or bar
Intro to Dependency Injection - Or bar Intro to Dependency Injection - Or bar
Intro to Dependency Injection - Or bar
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Lab#1 - Front End Development
Lab#1 - Front End DevelopmentLab#1 - Front End Development
Lab#1 - Front End Development
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
JavaScript Best Pratices
JavaScript Best PraticesJavaScript Best Pratices
JavaScript Best Pratices
 
Dagger 2 - Injeção de Dependência
Dagger 2 - Injeção de DependênciaDagger 2 - Injeção de Dependência
Dagger 2 - Injeção de Dependência
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
An Introduction to JavaScript
An Introduction to JavaScriptAn Introduction to JavaScript
An Introduction to JavaScript
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
Extending the build workflow of TFS 2010
Extending the build workflow of TFS 2010Extending the build workflow of TFS 2010
Extending the build workflow of TFS 2010
 
React – Structure Container Component In Meteor
 React – Structure Container Component In Meteor React – Structure Container Component In Meteor
React – Structure Container Component In Meteor
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
 
Best Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIBest Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part II
 
JavaScript Modules Done Right
JavaScript Modules Done RightJavaScript Modules Done Right
JavaScript Modules Done Right
 

En vedette

CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33Bilal Ahmed
 
ENG101- English Comprehension- Lecture 36
ENG101- English Comprehension- Lecture 36ENG101- English Comprehension- Lecture 36
ENG101- English Comprehension- Lecture 36Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45Bilal Ahmed
 
ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 44
MGT101 - Financial Accounting- Lecture 44MGT101 - Financial Accounting- Lecture 44
MGT101 - Financial Accounting- Lecture 44Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 08CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 08Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 15CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 15Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31Bilal Ahmed
 
ENG101- English Comprehension- Lecture 24
ENG101- English Comprehension- Lecture 24ENG101- English Comprehension- Lecture 24
ENG101- English Comprehension- Lecture 24Bilal Ahmed
 
ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43Bilal Ahmed
 
ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23Bilal Ahmed
 
MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41Bilal Ahmed
 

En vedette (20)

CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33
 
ENG101- English Comprehension- Lecture 36
ENG101- English Comprehension- Lecture 36ENG101- English Comprehension- Lecture 36
ENG101- English Comprehension- Lecture 36
 
MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45
 
ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32
 
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24
 
MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30MGT101 - Financial Accounting- Lecture 30
MGT101 - Financial Accounting- Lecture 30
 
MGT101 - Financial Accounting- Lecture 44
MGT101 - Financial Accounting- Lecture 44MGT101 - Financial Accounting- Lecture 44
MGT101 - Financial Accounting- Lecture 44
 
CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 08CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 08
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39
 
CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 15CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 15
 
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22
 
CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39CS101- Introduction to Computing- Lecture 39
CS101- Introduction to Computing- Lecture 39
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31
 
ENG101- English Comprehension- Lecture 24
ENG101- English Comprehension- Lecture 24ENG101- English Comprehension- Lecture 24
ENG101- English Comprehension- Lecture 24
 
ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 43
 
ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45ENG101- English Comprehension- Lecture 45
ENG101- English Comprehension- Lecture 45
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35
 
CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23
 
MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41
 

Similaire à CS101- Introduction to Computing- Lecture 32

Similaire à CS101- Introduction to Computing- Lecture 32 (20)

Event Programming JavaScript
Event Programming JavaScriptEvent Programming JavaScript
Event Programming JavaScript
 
Extjs
ExtjsExtjs
Extjs
 
IP Unit 2.pptx
IP Unit 2.pptxIP Unit 2.pptx
IP Unit 2.pptx
 
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSONAn introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
 
JS basics
JS basicsJS basics
JS basics
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Iwt note(module 2)
Iwt note(module 2)Iwt note(module 2)
Iwt note(module 2)
 
MTA understanding java script and coding essentials
MTA understanding java script and coding essentialsMTA understanding java script and coding essentials
MTA understanding java script and coding essentials
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Java script
Java scriptJava script
Java script
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
 
Java script
Java scriptJava script
Java script
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
JavaScripts & jQuery
JavaScripts & jQueryJavaScripts & jQuery
JavaScripts & jQuery
 
Java script
Java scriptJava script
Java script
 
Javascript
JavascriptJavascript
Javascript
 
Std 12 Computer Chapter 2 Cascading Style Sheet and Javascript (Part-2)
Std 12 Computer Chapter 2 Cascading Style Sheet and Javascript (Part-2)Std 12 Computer Chapter 2 Cascading Style Sheet and Javascript (Part-2)
Std 12 Computer Chapter 2 Cascading Style Sheet and Javascript (Part-2)
 
JavaScript
JavaScriptJavaScript
JavaScript
 

Plus de Bilal Ahmed

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19Bilal Ahmed
 

Plus de Bilal Ahmed (20)

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41
 
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38
 
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27
 
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25
 
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23
 
CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21
 
CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20
 
CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19
 

Dernier

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Dernier (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

CS101- Introduction to Computing- Lecture 32

  • 1. 1 CS101 Introduction to Computing Lecture 32Event Handling (Web Development Lecture 11)
  • 2. 2 During the last lecture we discussed Functions & Variable Scope • We looked at functions and their use for solving simple problems • We became familiar with a couple of JavaScript’s built-in functions • We became familiar with the concept of local and global variables
  • 3. 3 Function A group of statements that is put together (or defined) once and then can be used (by reference) repeatedly on a Web page Also known as subprogram, procedure, subroutine
  • 4. 4 Advantages of Functions • Number of lines of code is reduced • Code becomes easier to read & understand • Code becomes easier to maintain as changes need to be made only at a single location instead multiple locations
  • 5. 5 function writeList( heading, words ) { document.write( heading + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } } Keyword Function identifier Pair of parenthesis Function ‘arguments’ separated by commas Function definition enclosed in a pair of curly braces
  • 6. 6 Arguments of a Function • A comma-separated list of data • Arguments define the interface between the function and the rest of the Web page • Arguments values are passed to the function by value (some popular languages pass arguments ‘by reference’ as well)
  • 7. 7 To ensure that a function is defined before it is called up, define all functions in the HEAD portion of Web pages
  • 8. 8 Two Ways of Calling Functions function add( a, b ) { c = a + b ; return c ; } sum = add( 2, 4 ) ; document.write( sum ) ; function popUp( message ) { window.alert( message ) ; } popUp( “Warning!” ) ; A function call appearing as part of a statement. Definitions of such functions include a ‘return’ statement A function call appearing as a complete statement
  • 9. 9 What Would this Statement Do? factorial( factorial ( 3 ) ) ; This is termed as the recursive use of a function
  • 10. 10 Methods • Methods are functions • They are unusual in the sense that they are stored as properties of objects
  • 11. 11 Object: A named collection of properties prop 1 prop 2 prop 5 name prop 3 prop 4 A collection of properties All objects have the “name” property: it holds the name of the object (collection) prop 7prop 6 prop 8
  • 12. 12 Predefined, Top-Level or Built-In Functions • Event handlers are not the only functions that come predefined with JavaScript. There are many others. • Practically, there is no difference between predefined functions and those that are defined by the programmer (termed as user-defined or custom functions) • There are many of them, but here we discuss only two: parseInt( ), parseFloat( )
  • 13. 13 Local Variables • Declaring variables (using the var keyword) within a function, makes them local • They are available only within the function and hold no meaning outside of it
  • 14. 14 Local –vs- Global • Global variables can make the logic of a Web page difficult to understand • Global variables also make the reuse and maintenance of your code much more difficult HEURISTIC: If it’s possible to define a variable as local, do it!
  • 15. 15 Event Handlers • Special-purpose functions that come predefined with JavaScript • They are unusual in the sense that they are mostly called from the HTML part of a Web page and not the <SCRIPT> … </SCRIPT> part
  • 16. 16 Today’s Goal: Event Handlers • To become able to appreciate the concept of event handlers: – What are they? – What do they do? – How do we benefit from them? • To learn to write simple programs that use event handlers
  • 17. 17 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 clicking the mouse - or things that the system itself does - like updating the clock. Today we will exclusively focus on user-events
  • 18. 18 Event Driven Programs • Programs that can capture and respond to events are called ‘event-driven programs’ • JavaScript was specifically designed for writing such programs • Almost all programs written in JavaScript are event-driven
  • 19. 19 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 HTML code, and then directed to a JavaScript function for an appropriate response
  • 20. 20 Let’s now revisit lecture 15 where we introduced event handlers for the first time
  • 21. 21
  • 22. 22 <INPUT type=“submit” name=“sendEmail” value=“Send eMail” onMouseOver= “if (document.sendEmail.sender.value.length < 1) window.alert(‘Empty From field! Please correct’)” > Additional JavaScript code for the smart ‘Send eMail’ button that does not allow itself to be clicked if the “From” text field is left blank
  • 23. 23 That was event handling through what we may call ‘in-line JavaScript’ That is, the event was captured and handled with a JavaScript one-liner that was embedded in the HTML tag
  • 24. 24 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 3.A string consisting of JavaScript statements enclosed in double or single quotes
  • 25. 25 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 handling
  • 26. 26 Another - more sophisticated - way of accomplishing the same task
  • 27. 27 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.length < 1) { window.alert( “Empty From field! Please correct” ); } }
  • 28. 28 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 – It allows the reuse of a function for multiple event handlers
  • 29. 29 Another event-handling example; this time from lecture 18
  • 30. 30
  • 31. 31 onClick=“vuWindow()” JavaScript that goes between the <SCRIPT>, </SCRIPT> tags: JavaScript included as an attribute of the “New Window” button: function vuWindow() { window.open(“http://www.vu.edu.pk/”) ; }
  • 32. 32 A Few of My Favorite Event Handlers onClick onDblClick onMouseOver onMouseDown onFocus onBlur onReset onSubmit onLoad onUnload
  • 33. 33 There are many more: there is an expanded, but still incomplete list in your book Now let’s look at some of these error handlers in a bit more detail
  • 34. 34 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 when a window loses focus or a form element loses focus
  • 35. 35
  • 36. 36 <INPUT type="text" name="age" onBlur="checkAge( ) " > 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" ) ; } }
  • 37. 37 <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> </TR><TR> <TD><INPUT type="reset" value="Reset"></TD> <TD><INPUT type="submit" value="Submit"></TD></TR> </TABLE></FORM></BODY></HTML>
  • 38. 38 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 • What is the key difference between these 2 and the 4 event handlers (onMouseOver, onClick, onFocus, onBlur) that we have used so far?
  • 40. 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. 41 <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>
  • 42. 42 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 windows of a Web site (e.g. VULMS). onUnload can be used to warn that all child Windows will become inoperable if the user closes the parent Window
  • 43. 43 A Note on Syntax (1) • Mixed-case capitalization of event handlers (e.g. onClick) is a convention (but not a requirement) for JavaScript event handlers defined in HTML code. Using ‘ONCLICK’ or ‘onclick’ as part of a an HTML tag is perfectly legal as well
  • 44. 44 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. ‘onclick’ or ‘onmouseover’
  • 45. 45 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 • That statement is incorrect in two ways: 1. All four will work fine as part of HTML tags 2. Only the ‘all small case’ version will be interpreted as intended in JavaScript code
  • 46. 46 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 capabilities of a few event handlers
  • 47. 47 Next (the 12th ) Web Dev Lecture: Mathematical Methods • We’ll look at JavaScript’s Math object • We will produce solutions for simple problems using various methods of the Math object