Upload
shantam
View
220
Download
0
Embed Size (px)
Citation preview
8/12/2019 JavaScript to Finish
1/43
8/12/2019 JavaScript to Finish
2/43
8/12/2019 JavaScript to Finish
3/43
8/12/2019 JavaScript to Finish
4/43
8/12/2019 JavaScript to Finish
5/43
8/12/2019 JavaScript to Finish
6/43
8/12/2019 JavaScript to Finish
7/43
Common Uses of JavaScript
Basic Math Calculations
Dynamic Content Manipulation
Control Statement Form Validation
Navigation System
8/12/2019 JavaScript to Finish
8/43
8
Mathematical Functions in JavaScript
In addition to the simple arithmetic operations(e.g. +, *, etc.) JavaScript supports severaladvanced mathematicaloperations as well
Notationaly, these functions are accessedbyreferring to various methods of the Math object
Moreover, this object also contains severaluseful mathematical constantsas its properties
This object has no use, but of a placeholder
8/12/2019 JavaScript to Finish
9/43
9
Properties
Math.PIMath.E
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Math.SQRT2
Math.SQRT1_2
Note theCAPITAL
lettering
of allproperties
8/12/2019 JavaScript to Finish
10/43
10
Methods
random( )
sin( r )
cos( r )
tan( r )
asin( x )acos( x )
atan( x )
atan2( x, y )
max( x, y )
max( x, y )
round( x )
floor( x )
ceil( x )
exp( x )log( x ) abs( x )
sqrt( x )pow( x, y )
8/12/2019 JavaScript to Finish
11/43
11
sin(r),cos(r),tan(r)
Standard trigonometric functions
Returns the sine, cosine or tangent of r,
where r is specified in radians
EXAMPLE
document.write( Math.cos( Math.PI /4 ) )
0.7071067811865476
8/12/2019 JavaScript to Finish
12/43
8/12/2019 JavaScript to Finish
13/43
8/12/2019 JavaScript to Finish
14/43
8/12/2019 JavaScript to Finish
15/43
8/12/2019 JavaScript to Finish
16/43
Dynamic Content Manipulation
Using DOM (Document Object Model),
JavaScript can change the document in which
it is embedded
Elements can be move
Style can be changed
Visibility can be changed
8/12/2019 JavaScript to Finish
17/43
Nov 1
fit100-16-dom 2006 University of
Washington
What is the DOM?
Document Object Model Your web browser builds a modelof the web
page (the document) that includes all theobjectsin the page (tags, text, etc)
All of the properties, methods, and eventsavailable to the web developer for manipulatingand creating web pages are organized intoobjects
Those objects are accessible via scriptinglanguages in modern web browsers
8/12/2019 JavaScript to Finish
18/43
Nov 1
fit100-16-dom 2006 University of
Washington
What the heck is the DOM?
Document Object Model Your web browser builds a modelof the web
page (the document) that includes all theobjectsin the page (tags, text, etc)
All of the properties, methods, and eventsavailable to the web developer for manipulatingand creating web pages are organized intoobjects
Those objects are accessible via scriptinglanguages in modern web browsers
8/12/2019 JavaScript to Finish
19/43
slide 19
Example Page Manipulation
Some possibilities
createElement(elementName)
createTextNode(text)
appendChild(newChild)
removeChild(node)
Example: add a new list itemvar list = document.getElementById('t1')var newitem = document.createElement('li')
var newtext = document.createTextNode(text)
list.appendChild(newitem)
newitem.appendChild(newtext)
This uses the browserDocument Object Model
(DOM). We will focus on
JavaScript as a language,
not its use in the browser
8/12/2019 JavaScript to Finish
20/43
8/12/2019 JavaScript to Finish
21/43
8/12/2019 JavaScript to Finish
22/43
8/12/2019 JavaScript to Finish
23/43
8/12/2019 JavaScript to Finish
24/43
8/12/2019 JavaScript to Finish
25/43
8/12/2019 JavaScript to Finish
26/43
8/12/2019 JavaScript to Finish
27/43
8/12/2019 JavaScript to Finish
28/43
8/12/2019 JavaScript to Finish
29/43
8/12/2019 JavaScript to Finish
30/43
8/12/2019 JavaScript to Finish
31/43
8/12/2019 JavaScript to Finish
32/43
8/12/2019 JavaScript to Finish
33/43
Form Validation
Before an HTML form is submitted, JavaScript can
be used to provide client-side data validation
This is more user-friendly for the user than
server-side validation because itdoes not require a server round
trip before giving feedback
If the form is not valid, the formis not submitted until the errors
are fixed
8/12/2019 JavaScript to Finish
34/43
Client-Side Validation
JavaScript data validation happens before form is
submitted
Server-side application validation happens after the
form is submitted to the application server
Web Browser Web ServerApplication
Server
Database
Server
JavaScript
ValidationApplication
Validation
HTTP Request
HTTP Response
8/12/2019 JavaScript to Finish
35/43
8/12/2019 JavaScript to Finish
36/43
8/12/2019 JavaScript to Finish
37/43
8/12/2019 JavaScript to Finish
38/43
8/12/2019 JavaScript to Finish
39/43
Example password Validation
(HTML)
Password:
Confirm Password:
8/12/2019 JavaScript to Finish
40/43
Javascript methodfunction checkPass(){
//Store the password field objects into variables ...
var pass1 = document.getElementById('pass1');
var pass2 = document.getElementById('pass2');
//Store the Confimation Message Object ...
var message = document.getElementById('confirmMessage');
//Set the colors we will be using ...
var goodColor = "#66cc66";
var badColor = "#ff6666";
//Compare the values in the password field
//and the confirmation field
if(pass1.value == pass2.value){
//The passwords match.
//Set the color to the good color and inform
//the user that they have entered the correct password
pass2.style.backgroundColor = goodColor;
message.style.color = goodColor;
message.innerHTML = "Passwords Match!"
}else{
//The passwords do not match.
//Set the color to the bad color and
//notify the user.
pass2.style.backgroundColor = badColor;
message.style.color = badColor;
message.innerHTML = "Passwords Do Not Match!"
}
}
8/12/2019 JavaScript to Finish
41/43
41
Active site navigation
With JavaScript you can create a select and go
menu to select options from a drop down list
in one click, eliminating the go button
Take care not to confuse people who have
JavaScript turned off!
8/12/2019 JavaScript to Finish
42/43
42
Active site navigation
When the menu is not pulled down, it will
display a user prompt
This technique is used by many websites to
move the user to different sections
SelectAndGo.html
http://www.tech.port.ac.uk/staffweb/briggsj/WECPP/notes/javascript/examples/SelectAndGo.htmlhttp://www.tech.port.ac.uk/staffweb/briggsj/WECPP/notes/javascript/examples/SelectAndGo.html8/12/2019 JavaScript to Finish
43/43
Example
SelectAndGo.html
Function changePages(newLoc)
http://www.tech.port.ac.uk/staffweb/briggsj/WECPP/notes/javascript/examples/SelectAndGo.htmlhttp://www.tech.port.ac.uk/staffweb/briggsj/WECPP/notes/javascript/examples/SelectAndGo.html