Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JavaScript Programming an Introduction Prepared By P.D. Krolak and M.S. Krolak Based on material from JavaScript Unleashed (2nd Ed)"— Presentation transcript:

1

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

3 Introduction to JavaScript Basics for Non Programmers

4 JavaScript JavaScript is a Programming Language, although not a complete one. JavaScript was developed by Netscape and is becoming an international standard, ECMA-262. JavaScript is sent with the HTML document and is interpreted at the time it is loaded on the browser JavaScript adds interaction

5 Embedding JS in HTML

6 The tag container 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

7 The tag 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. 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.

8 The tag continued JavaScript libraries may be stored external to the HTML document. The Library file extension must be “.js” The External file does not have the tags

9 Viewing the JS code A Simple Hello World JS Sample document.write(“Hello World”); See the above web page

10 Viewing the JS code A Confirm Hello World JS Sample confirm(“Hello World”); See the above web page

11 Viewing the JS code An Alert window -- Hello World JS Sample alert(“Hello World”); See the above web page

12 Accommodating the Non JS supporting browsers The contents of the script container are commented so non JS browsers will ignore the contents. <! -- // the comment is used by the non-JS Your JavaScript code here -->

13 Executing the scripts The scripts are not executed in any necessary order. They are executed as the flow of events dictates. Events are generally created by an action of the user interacting with the page elements. An example, a mouse over a link.

14 Loading a web page There is no difference in loading a page with a script or without one.

15 HTML enhancements

16 JS Fundamentals

17 Versions of JS Version of JavaScriptBrowser Support Javascript 1.1Netscape 3. Javascript 1.2Netscape 4.0 Javascript 1.3Netscape 4.5

18 Versions of JS (cont.) Version of JavaScriptBrowser Support JavaScript 1.5 JS 1.5 is the current version Netscape 7. IE 6 JavaScript 1.5 has fewer issues between the two major browsers

19 JavaScript Data Types JavaScript does not have a rich set of data types like most widely used programming languages. JavaScript is also not strongly typed.

20 JavaScript Data Types DescriptionExamples String – a sequence of letters, numbers, or speical characters “a’ “abc’ ‘123abc’ Number – an integer or a floating point number 1 -123 123.45 123.456E+05

21 JavaScript Data Types DescriptionExamples BooleanTrue False

22 JavaScript Variables

23 JavaScript Function

24 JavaScript Expression

25 JavaScript Variables

26 JavaScript Literals

27 JavaScript Operators

28 JS Operators Assignment Operators Arithmetic Operators Comparison Operators String Operators Conditional Operators Boolean Operators The type of operators

29 Assignment Assignment operator assigns a value to a variable.

30 Assignment Operators OperatorDescription =Assigns the value on the right to the variable on the left of the =. +=. -=, *=, /=The right variable is operated on by the operand on the left using the indicated operator. &=, !=Assign the logical value to the right value using the right and the left operand and the indicated logical operator.

31 Example of Assignment:

32 Arithmetic Assignment AssignmentDescription x += yx= x+y addition x -=yx= x-y subtraction x *= yx= x*y x /= yx= x/y x %= yx= x%y x mod (y)

33 Arithmetic Arithmetic operators take numeric variables and return a single numeric value

34 Example of Arithmetic Ops:

35 Bitwise Operator Assignment AssignmentDescription x <<=yx= x<<y x >>=yx= x>>y x >>>= yx= x>>>y x &= yx= x&y x ^= yx= x^y x |= yx= x|y

36 Arithmetic Operators OperatorDescription % or Modulo function4%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

37 Comparison A comparison operator logically compares its operands and returns a logical True or False.

38 Comparison Operators OperatorDescription ==Equal !=Not Equal >Greater than >=Greater than or Equal <Less than <=Less than or Equal

39 Example of Comparison Ops:

40 String Operators String operators are the same as comparison, using a sort sequence. This is called a lexicographic or dictionary sort String a> b, or a>=b etc. String has two forms of concatenation, i.e. (the string ‘b’ is added to the end of string ‘a’) 1.a+b 2. a +=b

41 String Operator

42 Conditional Assign a value if the condition is true or the alternative value if not. (condition) ? True Value : False Value

43 Boolean A Boolean operator takes two operands and returns a truth value.

44 Boolean Operators OperatorDescription &&Logical And returns True if both operands are True. ||Logical OR returns True if either operand or both are True. !Logical NOT returns a True if the operand is False and returns False if operand is True.

45 Typeof Operand Typeof operand returns the data type of the operand.

46 JS Operators (continued) Function operators Data structure operators Bit wise operators Operator precedence

47 Function Operator

48 JavaScript Statements

49 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.

50 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 */

51 JS Control Structures and Looping Conditional statements Looping statements Labeled with switch

52 Conditional statements If …. else If (condition) {Block of Statements;} else {Block of Statements;}

53 Looping statements For The 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;}

54 Looping statements For … in On 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;}

55 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)

56 Looping statements While While executes as long as condition is true. Will not execute the first time if false. While (condition) { Block of Statements;}

57 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.

58 Labeled Label statement – allows the break and continue statement to transfer to this label. Label: statement;

59 with Establishes an object as the point of reference. with (object) {Block of Statement;}

60 switch Switch is the JavaScript case statement. Switch (expression){ case label-1: {Block of Statements;} break …. case label-n: {Block of Statements;} break default: {Block of Statements;} }

61 JavaScript Objects

62 JS as Object Orientated Language Object and Dot Notation Properties Methods Events

63 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

64 Object Dot Notation A property is: Object.property where dot reads is_a A method is: Object.method() where dot reads is_a

65 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.

66 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

67 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.

68 What are the JS’s objects JavaScript objects fall into classes –Navigator Objects that mostly correspond to HTML tags –Built-in Language Objects

69 JavaScript Objects & Corresponding HTML Tags

70 JS Object Hierarchy

71 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

72 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.

73 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.

74 JS Window Object The Window Object contains all the objects.

75 JS Navigator object Contains information about the browser. Properties can not be set by JavaScript.

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

77 JS History Properties & Methods MethodsDescription 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.

78 JS History Properties & Methods

79 JS History Example Create a back to last page link Go to Last Page In the above link tag the href is: 1. set to a JavaScript 2.Returns the link found in the history object using the back method with no argument, i.e. the default is the URL of the previous link. 3.When clicked the link returns to the previous link

80 JS Document Object Contains all the objects that are contained in the document. The objects that are contained correspond to the HTML tags found on the web page.

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

82 JS Frame Object Contains information about the frames in the widow.

83 Built-In Language Objects Built-in objects do not appear in the document but in the code. Built-in objects include: –Array –Date –Math –Regular Expression –String

84 Array Object

85 Date Object

86 Math Object

87 Regular Expression Objects

88 String Object

89 Properties

90 Methods

91 Events

92 JS Events and Event Handlers

93 User Defined Objects

94 JS Object Constructor A method is a constructor if it has the same name as the object an provides a template for creating an instance of the object. Function object(parm 1, … ){ This.property 1 = parm 1 ;.. This.method1= function1.. }

95 Instantiating Objects

96 Debugging JavaScript

97 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.

98 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.

99 Netscape JS Debugger Download the JS debugger from their web site: http://developer.netscape.com/software/jsdebug.html

100 JavaScript Applications

101 JavaScript References

102 Richard Wagner, et al. JavaScript Unleashed (Second Ed.), Sams.net (1997) Michael Moncur, SAMS Teach Yourself JavaScript in 24 Hours (Second Ed.), Sams Pubs. (2000). Ellie Quigley, JavaScript by Example, Prentice Hall, (2001).


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

Similar presentations


Ads by Google