Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.

Similar presentations


Presentation on theme: " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing."— Presentation transcript:

1  2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript Program: Adding Integers 7.4 Memory Concepts 7.5 Arithmetic 7.6 Decision Making: Equality and Relational Operators 7.7 JavaScript Internet and World Wide Web Resources

2  2001 Prentice Hall, Inc. All rights reserved. 2 JavaScript scripting language –Originally created by Netscape –Facilitates disciplined approach to designing computer programs –Enhances functionality and appearance of Web pages Jscript –Microsoft’s version of JavaScript

3  2001 Prentice Hall, Inc. All rights reserved. 3 Browser includes JavaScript Interpreter –Processes JavaScript commands Whitespace –Blank lines, space characters, tab characters –Generally ignored by browser –Used for readability and clarity … tag: –Encloses entire script –Attribute type = “text/javascript” Indicates scripting language (JavaScript default in IE5 & Netscape) –Attribute LANGUAGE=“JavaScript” is an old style

4  2001 Prentice Hall, Inc. All rights reserved. 4 document.writeln( “ string ” ); –Case-sensitive, like all JavaScript functions –Uses the writeln method in the browser’s document object –Prints the string, which can consist of any text and HTML tags –String must be surrounded by quotation marks ( “…” ) Correct method call syntax: –object.method( “string”, “[additional arguments]” ); Statement terminators –All statements must end with semi-colons ( ; ) A Simple Program: Printing a Line of Text in a Web Page

5  2001 Prentice Hall, Inc. All rights reserved. Outline 5 welcome.html Program Output 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 10 A First Program in JavaScript 11 12 13 <!-- 14 document.writeln( 15 " Welcome to JavaScript Programming! " ); 16 // --> 17 18 19 20 The document object’s writeln method writes a line of XHTML markup in the XHTML document. The script tag indicates to the browser that the text which follows is part of a script.

6  2001 Prentice Hall, Inc. All rights reserved. Outline 6 Welcome2.html Program Output 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 10 Printing a Line with Multiple Statements 11 12 13 <!-- 14 document.write( " " ); 15 document.write( "Welcome to JavaScript " + 16 "Programming! " ); 17 // --> 18 19 20 21 Using the style attribute, the color of the text is changed from black to magenta. The escape sequence \” places a quote in the string and is not displayed in the browser.

7  2001 Prentice Hall, Inc. All rights reserved. 7 How your browser translates? JavaScript Interpreter execute JavaScript code Browser renders HTML page HTML page with JavaScript page with HTML code only Rendered page

8  2001 Prentice Hall, Inc. All rights reserved. 8 writeln() vs. write() Object: document methods: –writeln Positions output cursor on next line when finished –write Leaves the output cursor where it is when done executing –Both begin output where previous statement stopped –Line breaks inserted in two ways: document.writeln( “Have a Nice Day!” )

9  2001 Prentice Hall, Inc. All rights reserved. Outline 9 Welcome3.html Program Output 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 Printing Multiple Lines 10 11 12 <!-- 13 document.writeln( " Welcome to JavaScript" + 14 " Programming! " ); 15 // --> 16 17 18 19 Using break tags, the text is displayed as three lines.

10  2001 Prentice Hall, Inc. All rights reserved. Outline 10 Welcome4.html 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 Printing Multiple Lines in a Dialog Box 10 11 12 <!-- 13 window.alert( "Welcome to\nJavaScript\nProgramming!" ); 14 // --> 15 16 17 18 19 20 Click Refresh (or Reload) to run this script again. 21 22 The window method alert displays an alert dialog to the user. When the alert dialog displays, the string passed as its one argument is displayed. The escape sequence \n is the newline character that places all remaining text on the next line.

11  2001 Prentice Hall, Inc. All rights reserved. 11 window object Methods in window object –Call on-screen windows –window.alert( “argument” ); Method calls alert window with window text " argument" Outputs button with text and ‘ OK’ button –window.prompt(“”); Prompts user for string (discussed later) Scripts restart when page reloaded/refreshed

12  2001 Prentice Hall, Inc. All rights reserved. 12 Reload /Refresh Scripts restart when page reloaded/refreshed.

13  2001 Prentice Hall, Inc. All rights reserved. Outline 13 Program Output The OK button allows the user to dismiss (or hide) the dialog. Title bar The dialog is automatically sized to accommodate the string. Mouse cursor

14  2001 Prentice Hall, Inc. All rights reserved. 14 7.2 Simple Program: Printing a Line of Text in a Web Page

15  2001 Prentice Hall, Inc. All rights reserved. 15 Variables –Location in memory where values are stored –Variable name can be any valid identifier Identifier = series of characters –Letters, digits, underscores (‘ _ ’) and dollar signs (‘ $ ’) –Cannot begin with a digit Valid identifiers: Welcome, $value, _value, m_inputField1, C3PO and R2D2 Invalid identifiers: 7button, Say\Hello and field#5 –Identifiers are case-sensitive Variables

16  2001 Prentice Hall, Inc. All rights reserved. 16 Variable name convention –Begin with lowercase first letter –Every following word has first letter capitalized goRedSox, bostonUniversityRules Declarations –var name1, name2 –Indicate that name1 and name2 are program variables

17  2001 Prentice Hall, Inc. All rights reserved. 17 Method window.prompt( “arg1”, “arg2” ) Calls window that allows user to enter value to use in the script arg1 : text that will appear in window arg2 : text that will initially appear in input line firstNumber = window.prompt(); Assigns value entered by the user in prompt window to variable firstNumber " =" a binary operator Assigns value of right operand to left operand window.prompt()

18  2001 Prentice Hall, Inc. All rights reserved. 18 Good programmers write many comments –Helps other programmers decode script –Aids debugging –Comment Syntax: One-line comment: // [text] Multi-line comment: /* [text] */ comments

19  2001 Prentice Hall, Inc. All rights reserved. 19 parseInt(); –Function accepts a string and returns an integer value Not a method because we do not refer to an object name number1 = parseInt( firstNumber ); –Operates right-to-left (due to the "=" sign) parseInt()

20  2001 Prentice Hall, Inc. All rights reserved. 20 sum = number1 + number2; –Adds number1 and number2 –Assigns result to variable sum String concatenation: –Combines string and another data type Other data type can be another string –Example: If age = 20, document.writeln( “I am ” + age + “years old!” ); Prints: I am 20 years old! + as addition and String concatenation operator

21  2001 Prentice Hall, Inc. All rights reserved. Outline 21 Addition.html 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 10 An Addition Program 11 12 13 <!-- 14 var firstNumber, // first string entered by user 15 secondNumber, // second string entered by user 16 number1, // first number to add 17 number2, // second number to add 18 sum; // sum of number1 and number2 19 20 // read in first number from user as a string 21 firstNumber = 22 window.prompt( "Enter first integer", "0" ); 23 24 // read in second number from user as a string 25 secondNumber = 26 window.prompt( "Enter second integer", "0" ); 27 28 // convert numbers from strings to integers 29 number1 = parseInt( firstNumber ); 30 number2 = parseInt( secondNumber ); 31 32 // add the numbers 33 sum = number1 + number2; 34 The window method prompt displays a prompt dialog in the browser with a message and a text field for input. The first argument passed to method prompt is the message to be displayed. The second argument is the default value for the text field. Function parseInt converts its string argument to an integer. The + operator adds the two numbers input by the user.

22  2001 Prentice Hall, Inc. All rights reserved. Outline 22 Addition.html Program Output 35 // display the results 36 document.writeln( " The sum is " + sum + " " ); 37 // --> 38 39 40 41 42 Click Refresh (or Reload) to run the script again 43 44

23  2001 Prentice Hall, Inc. All rights reserved. 23 7.3 Another JavaScript Program: Adding Integers When the user clicksOK, the value typed by the user is returned to the program as a string. The program must convert the string to a number. This is the text field in which the user types the value. This is the prompt to the user. This is the default value if the user does not enter a number. Fig. 7.7Prompt dialog displayed by the window object’s prompt method.

24  2001 Prentice Hall, Inc. All rights reserved. 24 7.4 Memory Concepts Fig. 7.8Memory location showing the name and value of variable number1. Fig. 7.9Memory locations after values for variables number1 and number2 have been input.

25  2001 Prentice Hall, Inc. All rights reserved. 25 Variables & Memory Concepts Variables: –Name corresponds to location in memory –Have 3 attributes: Name Type Value Memory –When a value assigned to a variable, it overwrites any previous value –Reading values is non-destructive sum = number1 + number2 Does not change number1 or number2

26  2001 Prentice Hall, Inc. All rights reserved. 26 7.5 Arithmetic Fig. 7.10Memory locations after calculating the sum of number1 and number2.

27  2001 Prentice Hall, Inc. All rights reserved. 27 Binary Operators –Used in arithmetic operations Modulus operator ( % ) –Yields remainder after division –Examples: 43 % 5 = 3 8.7 % 3.4 = 1.9 24 % 6 = 0 Operators

28  2001 Prentice Hall, Inc. All rights reserved. 28 7.5 Arithmetic

29  2001 Prentice Hall, Inc. All rights reserved. 29 Arithmetic

30  2001 Prentice Hall, Inc. All rights reserved. 30 operator precedence Arithmetic operations –Operate right to left (like the ‘ = ’ sign) Rules of operator precedence –Operations execute in a specific order

31  2001 Prentice Hall, Inc. All rights reserved. 31 Order of evaluation Step 1. Step 2. Step 3. Step 4. Step 5. Step 6.

32  2001 Prentice Hall, Inc. All rights reserved. 32 if structure: –Program makes decision based on truth or falsity of condition If condition met (true) –Statement(s) in body of if structure executed If condition not met (false) –Statement(s) in body of if structure skipped Format: if (condition) { statement; (additional statements); } Semi-colon (‘ ; ’) –Do not place after condition –Place after every statement in body of structure Decision Making: Equality and Relational Operators

33  2001 Prentice Hall, Inc. All rights reserved. 33 Equality and Relational Operators Equality and Relational Operators:

34  2001 Prentice Hall, Inc. All rights reserved. 34 Operator precedance Fig. 7.13Order in which a second-degree polynomial is evaluated.

35  2001 Prentice Hall, Inc. All rights reserved. 35 7.6 Decision Making: Equality and Relational Operators

36  2001 Prentice Hall, Inc. All rights reserved. Outline 36 Comparison.html 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 8 9 10 11 Performing Comparisons 12 13 14 <!-- 15 var first, // first string entered by user 16 second, // second string entered by user 17 number1, // first number to compare 18 number2; // second number to compare 19 20 // read first number from user as a string 21 first = window.prompt( "Enter first integer:", "0" ); 22 23 // read second number from user as a string 24 second = window.prompt( "Enter second integer:", "0" ); 25 26 // convert numbers from strings to integers 27 number1 = parseInt( first ); 28 number2 = parseInt( second ); 29 30 document.writeln( " Comparison Results " ); 31 document.writeln( 32 " " ); 33 Two prompt dialogs retrieve user input.

37  2001 Prentice Hall, Inc. All rights reserved. Outline 37 Comparison.html 34 if ( number1 == number2 ) 35 document.writeln( " " + number1 + " == " + 36 number2 + " " ); 37 38 if ( number1 != number2 ) 39 document.writeln( " " + number1 + " != " + 40 number2 + " " ); 41 42 if ( number1 < number2 ) 43 document.writeln( " " + number1 + " < " + 44 number2 + " " ); 45 46 if ( number1 > number2 ) 47 document.writeln( " " + number1 + " > " + 48 number2 + " " ); 49 50 if ( number1 <= number2 ) 51 document.writeln( " " + number1 + " <= " + 52 number2 + " " ); 53 54 if ( number1 >= number2 ) 55 document.writeln( " " + number1 + " >= " + 56 number2 + " " ); 57 58 // Display results 59 document.writeln( " " ); 60 // --> 61 62 63 64 65 Click Refresh (or Reload) to run the script again 66 67 Each if statement uses the equality operators to determine the relationship of the two integers input by the user.

38  2001 Prentice Hall, Inc. All rights reserved. 38 If: First Integer = 123 Second Integer = 123 If: First Integer = 100 Second Integer = 200 If: First Integer = 200 Second Integer = 100

39  2001 Prentice Hall, Inc. All rights reserved. Outline 39 Program Output

40  2001 Prentice Hall, Inc. All rights reserved. Outline 40 Program Output

41  2001 Prentice Hall, Inc. All rights reserved. Outline 41 Program Output

42  2001 Prentice Hall, Inc. All rights reserved. 42 7.6 Decision Making: Equality and Relational Operators


Download ppt " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing."

Similar presentations


Ads by Google