JavaScript II
description
Transcript of JavaScript II
![Page 1: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/1.jpg)
JavaScript II
ECT 270
Robin Burke
![Page 2: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/2.jpg)
Outline
Functions Events and event handling Form validation
![Page 3: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/3.jpg)
Functions
In Javayou wrote functions associated with
classes• methods
In JavaScriptwe won't be defining new classeswe write functions with global scope
• can be called anywhere on the pagefunctions can also simplify programs
![Page 4: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/4.jpg)
Functions cont'd
Syntaxfunction name(parameter list)
{
... function code ...
}
Placementbest place is in the HEAD element
• recall skeleton
must be before first invocation
![Page 5: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/5.jpg)
Function cont'd
Functions need not return a value different from VB
If a function does return a value, usereturn value;
Scope variables declared with var inside a function
• are local
different from Java• all variables in a method are local
![Page 6: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/6.jpg)
Example
slide show with functions
![Page 7: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/7.jpg)
Events
Event handling makes JavaScript useful
Examplesrollover effectsgo to a page on menu selectionvalidate the contents of a form
![Page 8: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/8.jpg)
Basic idea
Events are generated user actions (clicking, submitting a form
moving mouse) browser actions (loading, closing)
To use an event, the programmer writes JavaScript code associates it with the appropriate document
element associates it with the appropriate event
![Page 9: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/9.jpg)
Event-driven programs
Modern UIs are all event-driven Different kind of programming from sequential (batch)
execution programmer does not control when code is executed user controls that
Programmer provides capabilities the user invokes them may be multiple ways to do the same thing
Basic idea = "event handlers" small bits of code that the application calls when certain events occur
![Page 10: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/10.jpg)
Event-driven programming
Imperative program load payroll data do payroll computation output checks
Event-oriented program establish payroll application interface associate loading routine with "load" menu item associate payroll computation with "compute" menu
option associate check printing operation with "print" menu
options User is in charge of the sequence
![Page 11: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/11.jpg)
Application
Using form buttons as event generatorswe're not interested in form behavior<input type="button" onClick="...code
here..."> When user clicks the button
code executed• typically a function call
![Page 12: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/12.jpg)
"this"
Useful to know which element generated the event<img ... onClick="foo(this)">
When foo will be calledwith one argument= the image element clicked the user
![Page 13: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/13.jpg)
Example
slide show with buttons
![Page 14: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/14.jpg)
Events for elements
onClick onDblClick onMouseOver onMouseOut
![Page 15: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/15.jpg)
Syntax for action links
href="javascript:code" Example
<a href="javascript:myFn();">
![Page 16: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/16.jpg)
Events for windows
onLoad onUnload onResize
![Page 17: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/17.jpg)
Events for forms
for input elementsonFocusonBluronSelectiononChange
for the form itselfonSubmitonReset
![Page 18: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/18.jpg)
Example
rollover on the slide show buttonsboldred
handling menu selectionmenu of URLsmenu of images
![Page 19: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/19.jpg)
Form validation
Problem detecting erroneous input round-trip to server too slow HTML controls limited
Solution use JavaScript to detect bad input get user to correct before hitting the server
Note an efficiency tool not a data quality guarantee server must validate the data again to easy to generate a rogue request
![Page 20: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/20.jpg)
Technique
Use onSubmit event Special syntax
onSubmit="return fn()" if fn returns true
form data sent to server if fn returns false
form data not sent
![Page 21: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/21.jpg)
Creating events
We can generate form eventsfrom within JavaScript code
Useful to control the
![Page 22: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/22.jpg)
Example
Change of password
![Page 23: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/23.jpg)
Form contents
For validationwe need to be able to extract the
contents from the form Navigating
getting to the element Extracting information
different techniques for each widget
![Page 24: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/24.jpg)
Navigating the document
JavaScript nativerepresents content in arraysaccessible by numberaccessible by nameindex notation optional for names
Can be confusing
![Page 25: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/25.jpg)
Examples
Assume "myform" is the first form in a document document.forms collection
and other collections document.forms[0] documents.forms["myform"] documents.forms.myform
document.tag_name document.form1 works because form1 is a "top-level" element
document.all collection document.all[1] document.all["myform"] document.all.myform
document.all.tags collection document.all.tags("FORM")[0]
![Page 26: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/26.jpg)
That's not all!
JavaScript has been unified with the W3C DOMdocument object modelworld-wide web consortium
A whole additional suite of document navigation methods
We will talk about these next week
![Page 27: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/27.jpg)
Example
form validation for APGARinsert correct datehandle "other" doctors namecalculating totalsreal-time entry checkconsent checkbox
![Page 28: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/28.jpg)
Handling selections
Get the select element document.formname.selectname
Which is selected selectobj.selectedIndex
options collection options[0] is the first option
An option value = what is passed to the server text = what is displayed selected = true if it is selected
![Page 29: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/29.jpg)
To add new name
When What to do
![Page 30: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/30.jpg)
Trickier
Adding the new name to the menu
![Page 31: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/31.jpg)
Create totals
When What
![Page 32: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/32.jpg)
Checkbox
When What
![Page 33: JavaScript II](https://reader035.fdocuments.in/reader035/viewer/2022081603/568145ee550346895db2f1b9/html5/thumbnails/33.jpg)
Homework #7