Beruflich Dokumente
Kultur Dokumente
Outline
1. 2. 3. 4. 5. 6. 7. 8. Introduction Simple Program: Printing a Line of Text in a Web Page Data types Obtaining User Input with prompt Dialogs Memory Concepts Arithmetic Decision Making: Equality and Relational Operators Web Resources
Objectives
In this lesson, you will learn:
To be able to write simple JavaScript programs. To be able to use input and output statements. To understand basic memory concepts. To be able to use arithmetic operators. To understand the precedence of arithmetic operators. To be able to write decision-making statements. To be able to use relational and equality operators.
1 Introduction
JavaScript scripting language
Enhances functionality and appearance Client-side scripting
Makes pages more dynamic and interactive
Foundation for complex server-side scripting Allows us to develop webpages that act as programs Gives us more control than normal HTML/CSS
1 Introduction
JavaScript is the backbone of Web 2.0 Web 2.0 is more than just visual style The second generation of internet
Communities and information sharing User centred design to facilitate creativity A lot more control given to users
Examples include wikis, blogs and social networking sites Often highly customisable with a widget based approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -strict.dtd">
-->
<html xmlns = "http://www.w3.org/1999/xhtml" > <head> <title>A First Program in JavaScript </title>
welcome.html (1 of 1)
<!-document.writeln( // --> </script> </head><body></body>
20 </html>
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -strict.dtd">
-->
<html xmlns = "http://www.w3.org/1999/xhtml" > <head> 10 11 12 13 14 15 16 17 18 19 20 </head><body></body> <title>Printing a Line with Multiple Statements </title>
welcome2.html (1 of 1)
<!-"Programming!</h1>" ); // --> </script>
21 </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -strict.dtd">
--> -->
welcome3.html 1 of 1
<!-// --> </script> </head><body></body>
19 </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -strict.dtd">
-->
<html xmlns = "http://www.w3.org/1999/xhtml" > <head><title> Printing Multiple Lines in a Dialog Box </title>
welcome4.html 1 of 1
<!-// --> </script> </head> <body>
<p>Click Refresh (or Reload) to run this script again. </p> </body>
22 </html>
Description Newline. Position the screen cursor at the beginning of the next line. Horizontal tab. Move the screen cursor to the next tab stop. Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the characters previously output on that line. Backslash. Used to represent a backslash character in a string. Double quote. Used to represent a double quote character in a string contained in double quotes. For example,
window.alert( "\"in quotes\"" ); displays "in quotes" in an alert dialog.
\\ \"
\'
Single quote. Used to represent a single quote character in a string. For example,
window.alert( '\'in quotes\'' ); displays 'in quotes' in an alert dialog.
Data types
Numbers - refers to numerical values Strings - refers to one or more characters of text Booleans True or false values Undefined or null - variables or object properties that are either undefined or do not exist
Numbers
As the name implies, a number type refers to numerical values. Numbers can be divided into two groups: floating-point are fractional numbers such as 2.45, -3.58, and .97 Integers are whole numbers such as 245, -480, and 3.
Strings
string is enclosed inside single or double quotes. Examples of strings:
University of Sydney", 'HTML', 'JavaScript', and 2007", are all examples of strings.
Boolean
A Boolean value is a truth value, either 'true' or 'false', often coded 1 and 0, respectively. Boolean logic allows your program to make decisions. A Boolean logic statement consists of a condition that evaluates to either true or false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
-->
welcome5.html (1 of 2)
<!--
// read the name from the prompt box as a string name = window.prompt( "Please enter your name", "GalAnt" );
document.writeln( "<h1>Hello, " + name + ", welcome to JavaScript programming!</h1>" ); // --> </script>
23 24 25 26 27 28 <body> <p>Click Refresh (or Reload) to run this script again. </p> </body> </head>
29 </html>
welcome5.html (2 of 2)
This is the default value that appears when the dialog opens.
This is the text field in which the user types the value.
Fig. 7.7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-->
Addition.html (1 of 2)
<!-var firstNumber, secondNumber, number1, number2, sum;
// first string entered by user // second string entered by user // first number to add // second number to add // sum of number1 and number2
// read in first number from user as a string firstNumber = window.prompt( "Enter first integer", "0" );
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// read in second number from user as a string secondNumber = window.prompt( "Enter second integer", "0" );
// convert numbers from strings to integers number1 = parseInt( firstNumber ); number2 = parseInt( secondNumber );
Addition.html (2 of 2)
// display the results // --> </script> </head> <body> </body>
44 </html>
4 Memory Concepts
Variable names correspond to locations in the computers memory Every variable has a name, a type, and a value Read value from a memory location
nondestructive
4 Memory Concepts
number1
45
Fig. 7.9
4 Memory Concepts
number1 number2 45 72
Fig. 7.10
Memory locations after values for variables number1 and number2 have been input.
4 Memory Concepts
45 72 117
5 Arithmetic
Many scripts perform arithmetic calculations
Expressions in JavaScript must be written in straight-line form
5 Arithmetic
JavaScript operation Addition
Subtraction Multiplication Division
Arithmetic operator
+ * /
JavaScript expression
f + 7 p - c b * m x / y r % s
Operation(s) Order of evaluation (precedence) Multiplication Evaluated second. If there are several such Division operations, they are evaluated from left to right. Modulus + or Addition Evaluated last. If there are several such operations, Subtraction they are evaluated from left to right. Fig. 7.13 Precedence of arithmetic operators.
Operator(s) *, / or %
5 Arithmetic
Step 1. y = 2 * 5 * 5 + 3 * 5 + 7; 2 * 5 is 10 (Leftmost multiplication) Step 2. y = 10 * 5 + 3 * 5 + 7; 10 * 5 is 50 (Leftmost multiplication)
Step 6. y = 72;
Fig. 7.14
u e
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
welcome6.html (1 of 3)
<!-now = new Date(),
var name, // string entered by the user // current date and time
// read the name from the prompt box as a string name = window.prompt( "Please enter your name", "GalAnt" );
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// determine whether the time is PM if ( hour >= 12 ) { // convert to a 12 hour clock hour = hour - 12;
welcome6.html (2 of 3)
if ( hour >= 6 ) } document.writeln( name + // --> </script>
</head>
47 48 49 50
<body> <p>Click Refresh (or Reload) to run this script again. </p> </body> </html>
welcome6.html (3 of 3)
Recommended Reading
W3schools - http://www.w3schools.com/js/default.asp ScriptMaster http://www.scriptingmaster.com/javascript/scriptingjavascript.asp HTML Goodies, JavaScript Basics http://www.htmlgoodies.com/primers/jsp/article.php/358641 1l