+ All Categories
Home > Documents > CISC474 - JavaScript

CISC474 - JavaScript

Date post: 15-Feb-2016
Category:
Upload: eris
View: 52 times
Download: 0 times
Share this document with a friend
Description:
CISC474 - JavaScript. 03/02/2011. Some Background…. Great JavaScript Guides: https://developer.mozilla.org/en/JavaScript/Guide http://www.w3schools.com/js/default.asp Client Side Scripting AJAX (Asynchronous JavaScript And XML) – has brought JavaScript back to mainstream. - PowerPoint PPT Presentation
24
CISC474 - JavaScript 03/02/2011
Transcript
Page 1: CISC474 - JavaScript

CISC474 - JavaScript

03/02/2011

Page 2: CISC474 - JavaScript

Some Background…

• Great JavaScript Guides:– https://developer.mozilla.org/en/JavaScript/Guide– http://www.w3schools.com/js/default.asp

• Client Side Scripting• AJAX (Asynchronous JavaScript And XML) –

has brought JavaScript back to mainstream

Page 3: CISC474 - JavaScript

JavaScript vs. Java

Page 4: CISC474 - JavaScript

What is JavaScript Used for?

• Handling User Interaction– Doing small calculations– Checking for accuracy and appropriateness of data entry from forms– Doing small calculations/manipulations of forms input data– Search a small databased embedded in the downloaded page– Save data as cookie so it is there upon visiting the page

• Generating Dynamic HTML documents• Examples

– Bookmarklets– Google Maps– Google Suggest

Page 5: CISC474 - JavaScript

DOM• JavaScript is Object-Oriented• JavaScript Interacts with Document Object Model• DOM Not Totally Standardized

Page 6: CISC474 - JavaScript

JavaScript Syntax - Variables and Literals

• Dynamic Typing - Variables can hold any valid type of value:– Number ... var myInt = 7; – Boolean ... var myBool = true; – Function ... [Discussed Later] – Object ... [Discussed Later] – Array ... var myArr = new Array(); – String ... var myString = "abc";– null, a special keyword denoting a null value; null is also a

primitive value. Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant

– undefined, a top-level property whose value is undefined; undefined is also a primitive value.

– ... and can hold values of different types at different times during execution

Page 7: CISC474 - JavaScript

Control Structures• ‘if’ statement

if ( boolean statement ) { ...} else { ...}

• ‘switch’ statementswitch ( myVar ) { case 1:// if myVar is equal to 1 this is executed case "two":// if myVar is equal to "two" this is executed case default:// if none of the cases above are satisfied OR if no // ‘break’ statement are used in the cases above, // this will be executed}

Page 8: CISC474 - JavaScript
Page 9: CISC474 - JavaScript

Assignment Operators

Page 10: CISC474 - JavaScript

Math Operators

Page 11: CISC474 - JavaScript

JavaScript Functionsfunction function_name(parameters) {

JavaScript commands}

– parameters are the values sent to the function (note: not all functions require parameters)

– { and } are used to mark the beginning and end of the commands in the function.

Page 12: CISC474 - JavaScript

JavaScript Functions

• Function names are case-sensitive.• The function name must begin with a letter or

underscore ( _ ) and cannot contain any spaces.• There is no limit to the number of function

parameters that a function may contain.• The parameters must be placed within

parentheses, following the function name, and the parameters must be separated by commas.

Page 13: CISC474 - JavaScript

Function Examples

Page 14: CISC474 - JavaScript

Where to Place Functions…• The function definition must be placed before

the command that calls the function.• One convention is to place all of the function

definitions in the <head> section.• A function is executed only when called by

another JavaScript command.• It’s common practice for JavaScript

programmers to create libraries of functions located in external files.

Page 15: CISC474 - JavaScript

Conditional Statementsif (condition) { JavaScript Commands}– condition is an expression that is either true or

false– if the condition is true, the JavaScript Commands

in the command block are executed– if the condition is not true, then no action is

taken

Page 16: CISC474 - JavaScript

Comparison, Logical, and Conditional Operators

To create a condition, you need one of three types of operators:– a comparison operator compares the value of

one element with that of another, which creates a Boolean expression that is either true or false

– a logical operator connects two or more Boolean expressions

– a conditional operator tests whether a specific condition is true and returns one value if the condition is true and a different value if the condition is false

Page 17: CISC474 - JavaScript

JavaScript Comparison Operators

Page 18: CISC474 - JavaScript

JavaScript Logical Operators

Page 19: CISC474 - JavaScript

A Conditional Operatortests whether a specific condition is true and returns one value if the condition is true and a different value if the condition is false.– Message = (mail == "Yes") ? "You have

mail": "No mail";– tests whether the mail variable is equal to the

value "Yes"• if it is, the Message variable has the value

"You have mail";• otherwise, the Message variable has the

value "No mail".

Page 20: CISC474 - JavaScript

If...Else Statementif (condition) { JavaScript Commands if true} else JavaScript Commands if false}– condition is an expression that is either true or

false, and one set of commands is run if the expression is true, and another is run if the expression is false

Page 21: CISC474 - JavaScript

Arrays• An array is an ordered collection of values

referenced by a single variable name.• The syntax for creating an array variable is:

var variable = new Array(size);– variable is the name of the array variable– size is the number of elements in the array

(optional)• To populate the array with values, use:

variable[i]=value;where i is the ith item of the array. The 1st item has an index value of 0.

Page 22: CISC474 - JavaScript

ArraysTo create and populate the array in a single statement, use:var variable = new Array(values);– values are the array elements enclosed in

quotes and separated by commas– var MonthTxt=new Array("January",

"February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");• What will January’s index value be?

Page 23: CISC474 - JavaScript

For Loopsfor (start; condition; update) { JavaScript Commands}– start is the starting value of the counter– condition is a Boolean expression that must be

true for the loop to continue– update specifies how the counter changes in

value each time the command block is executed

Page 24: CISC474 - JavaScript

While Loops• The While loop runs a command group as long

as a specific condition is met, but it does not employ any counters.

• The general syntax of the While loop is:while (condition) { JavaScript Commands}– condition is a Boolean expression that can be

either true or false


Recommended