javascript programming an introduction prepared by p .d. krolak and m.s. krolak

76
JavaScript Programming an Introduction Prepared By P .D. Krolak and M.S. Krolak Based on material from JavaScript Unleashed (2nd Ed)

Upload: amber-drake

Post on 30-Dec-2015

51 views

Category:

Documents


0 download

DESCRIPTION

JavaScript Programming an Introduction Prepared By P .D. Krolak and M.S. Krolak. Based on material from JavaScript Unleashed (2nd Ed). JavaScript Objects. Objects hold data Objects have Properties that can be set and have values Objects can have Methods. JavaScript. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript Programming an Introduction

Prepared ByP .D. Krolak and M.S. Krolak

Based on material from

JavaScript Unleashed (2nd Ed)

Page 2: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript Objects• Objects hold data

•Objects have Properties that can be set and have values

•Objects can have Methods

Page 3: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript

• JavaScript is a Programming Language, although not a complete one.

• JavaScript was developed by Netscape and is becoming an international standard

• JavaScript is sent with the HTML document and is interpreted at the time it is loaded on the browser

• JavaScript adds interaction

Page 4: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript Objects

• JS is not true Object Oriented Programming (OOP) but object like

• What are the JS’s objects

• What is the JS object Hierarchy

• Built-In Language Objects

Page 5: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS is not true OOP but object like.

• JS objects do have properties and methods like and object orientated language.

• JS objects do not support inheritance.

• JS object model is a container model not a class model.

• Container objects contain other containers.

Page 6: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

What are the JS’s objects

• JavaScript objects fall into classes– Navigator Objects that mostly correspond to

HTML tags– Built-in Language Objects

Page 7: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript Objects & Corresponding HTML Tags

window N/A

frame <frame>

document <body>

form <form>

button <Input Type=”button”>

checkbox <Input Type=”checkbox”>

fileUpload <Input Type=”file”>

Page 8: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Object Hierarchy

Page 9: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Window Object

• The Window Object contains all the objects.

Page 10: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Navigator object

• Contains information about the browser.

• Properties can not be set by Javascript.

Page 11: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS History Object

• The History object records the documents that were displayed in order of their presentation – most recent descending to oldest.

Page 12: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS History Properties & Methods

Methods Description

Back () Go back n pages – no argument go to last page

Forward() Go forward n pages if possible,

Go() Go N pages where n >0 for forward and n<=0 for back.

Page 13: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS History Properties & Methods

Page 14: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS History Example

Create a back to last page link

<a HREF=“javascript:history.back();”>

Go to Last Page </a>

Page 15: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Document Object

• Contains all the object that are contained in the document. The objects that are contained correspond to the HTML tags found on the web page.

Page 16: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Location Object

• Location Object contains the information about the source of the document, i.e. the URL, and related information.

Page 17: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Frame Object

• Contains information about the frames in the widow.

Page 18: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Built-In Language Objects

• Built Objects do not appear in the Document but in the code.

• Built-in Objects include:– Array– Date– Math– String

Page 19: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Array Object

Page 20: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Date Object

Page 21: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Math Object

Page 22: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

String Object

Page 23: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript Properties

• Properties in JS resemble data attributes of an object

• Properties explain the characteristics and identity of the given object

• Properties can represent the state of the object

• Properties could represent the role that the object plays at a given time

Page 24: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript Events & Event Handlers

• A javaScript event is controlled by browser based on action normally initiated by the viewer.

• Each tag has an associated set of event handlers that are triggered by a corresponding event. When the event handler is triggered, a javaScript is preformed.

Page 25: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript Methods

• Methods are functions that provide services for the object

1. Set the value of a property

2. Get the value of a property

3. Iterate on the object’s properties

4. Constructor for the object.

Page 26: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Embedding JS in HTML

Page 27: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Embedding JS in HTML

• The <script> tag• Accommodating the Non JS supporting browsers

by using a comment to surround the content of the script within the container.

• Viewing the JS code• Executing the scripts• loading a page• HTML enhancements

Page 28: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

The <script> tag

• <script language=“JavaScript1.x”> where x can be version of JavaScript used, i.e 0,1,2,3. If the browser does not support the version the tag and its content is ignored.

• language can also be set to vb script, php, etc.• <Script > </Script> form the container• The script may be placed anywhere in the document but

must exit before needed.• Most scripts are placed in the head and are thus loaded

before they are needed for the display & interaction.• Scripts that contain document.write() must be in the

body.

Page 29: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

The <script> tag continued

• JavaScript libraries may be stored external to the HTML document.

• <script Language=javascript1.2 src=“Library’s_URL”>

• Library file extension must be “.js”

Page 30: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Accommodating the Non JS supporting browsers

• The contents of the script container are commented so non JS browsers will ignore the contents.

• <Script ><! -- // the comment is used by the non-JS Your javaScript code here--></Script>

Page 31: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Viewing the JS code

<Html>

<Head>

</Head>

<Body>

<script Language=“javascript1.2”>

document.write(“Hello World”);

</Script>

</Body>

</Html>

Page 32: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Executing the scripts

• The scripts are not executed in any necessary order.

• They are executed as the flow of events dictates.

Page 33: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Loading a page

• There is no difference in loading a page with a script or without.

Page 34: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

HTML enhancements

Page 35: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Fundamentals

Page 36: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Versions of JS

Version of Javascript Browser Support

Javascript 1.1 •Netscape 2.

Javascript 1.2 Netscape

Javascript 1.3 Netscape 4.

Page 37: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Operators

• Assignment Operators

• Arithmetic Operators

• Comparison Operators

• String Operators

• Conditional Operators

• Boolean Operators

• The type of operators

Page 38: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Arithmetic Assignment

Assignment Description

x += y x= x+y addition

x -=y x= x-y subtraction

x *= y x= x*y

x /= y x= x/y

x %= y x= x%y x mod (y)

Page 39: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Assignment

Page 40: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Arithmetic

Page 41: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Arithmetic Operators

Operator Description

% or Mod 4%3 returns 1

++ Increment ++x sets x= x+1 and returns x+1 but x++ sets x=x+1 and returns x

-- Decrement --x sets x= x-1 and returns x-1 but x-- sets x=x-1 and returns x

- (uninary negation) Sets x = -x ,I.e reverses sign

Page 42: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Comparison

Page 43: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Comparison Operators

Operator Description

== Equal

!= Not Equal

> Greater than

>= Greater than or Equal

< Less than

<= Less than or Equal

Page 44: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

String

• String operators are the same as comparison, using a sort sequence

String a> b, or a>=b etc.

String has two forms of concatenate

1. a+b

2. a +=b

Page 45: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

String Operator

Page 46: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Conditional

Page 47: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Boolean

Page 48: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Operators (continued)

• Function operators

• Data structure operators

• Bit wise operators

• Operator precedence

Page 49: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Function Operator

Page 50: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Statements

• Statements define the flow of the script.• JS statements require a “;” at the end if

there are more than one on a line. • A block of statement that are executed in

order is enclosed by curly brackets {}.• Flow is normally linear but may be altered

by conditional, looping, or similar statements.

Page 51: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Comments

• Comments are not really part of the program statement but are provided for the programmer and others as notes and reminders of what is happening in the JS.

Statement; //single line comment

/* this the way to

create multi line comments */

Page 52: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Control Structures and Looping

• Conditional statements

• Looping statements

• Labeled

• with

• switch

Page 53: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Conditional statements

If …. else

If (condition)

{Block of Statements;}

else

{Block of Statements;}

Page 54: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Looping statements

ForThe traditional for loop loops until the test condition

is false. The initial statement is executed once. The test is applied and if true, the Block of Statements are executed. The counter is incremented and test applied. Repeat looping.

for (initial_statement; test_condition; increment)

{Block of Statements;}

Page 55: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Looping statements

For … inOn each iteration get one property of the

object. If the object has no properties the loop is not executed.

for (property in object)

{Block of Statements;}

Page 56: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Looping statements

do …. while

Repeat the block statement until the condition is false.

(Note it goes thru loop at least once)

do

{ Block of Statements; }

while (condition)

Page 57: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Looping statements

While

While executes as long as condition is true. Will not execute the first time if false.

While (condition)

{ Block of Statements;}

Page 58: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Break & Continue Statements

• Break – Drop out of loop and go to statement following loop.

• Continue – Drop out of the loop block of statements and go to the loop control. Looping continues until control is false.

Page 59: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Labeled

• Label statement – allows the break and continue statement to transfer to this label.

Label: statement;

Page 60: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

with

• Establishes an object as the point of reference.

with (object)

{Block of Statement;}

Page 61: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

switch

Switch is the JavaScript case statement.Switch (expression){

case label-1:{Block of Statements;}break….case label-n:{Block of Statements;}breakdefault:{Block of Statements;}

}

Page 62: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS as Object Orientated Language

• Object and Dot Notation

• Properties

• Methods

• Events

Page 63: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Object and Dot Notation

• The object is described from the container that holds the container that holds .. the container. Read from left to right and the dot as contains.

Example the form object from the diagram

Window.document.form

Page 64: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Object Dot Notation

• A property is:

Object.property where dot reads is_a

• A method is:

Object.method() where dot reads is_a

Page 65: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Object Dot Notation

• Since a window contains everything, it is called self.

• In the Dot notation we can drop the window because it is contained by nothing and contains everything.

Page 66: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Properties

Page 67: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Methods

Page 68: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Events

Page 69: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Events and Event Handlers

Page 70: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JS Object Constructor

• A method is a constuctor if it has the same name as the object an provides a template for creating an instance of the object.

Function object(parm1, … ){

This.property1 = parm1 ;..This.method1= function1..}

Page 71: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Instantiating Objects

Page 72: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Debugging JavaScript

Page 73: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Debugging JavaScript

• First sign of trouble will appear as error message in the status window (lower left bottom of window).

When a error does appear then type:

Javascript: in the location window (where the document URL goes). Note the last character “:” is required. A series of error messages that will attempt to locate the error will appear in the dialog window.

Page 74: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

When creating JavaScript turn Javascript Console automatically 1. Can not be done through preference option But it can

done.

2. Go to the pref.js in the Netscape directory.

3. Open in a text editor.

4. Last line add:user_pref(“javascript.console.open_on_error”,

true);

5. Save file and Restart Netscape.

Console should pop up when a javascript error occurs.

Page 75: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

Netscape JS Debugger

• Download the JS debugger from their web site:

http://developer.netscape.com/software/jsdebug.html

Page 76: JavaScript Programming an Introduction  Prepared By P .D. Krolak and M.S. Krolak

JavaScript References