XPReview• Declare a variable• Give example of a Numeric, String, Boolean Variable– Assign a value to it• What does this do: x = x+1• What does this do: x++• What does this code do:Tutorial 10 New Perspectives on HTML and XHTML, 1ComprehensiveXPCreating JavaScript Functions• A function is a series of commands that perform an action or calculates a value.• A function name identifies a function.• Parameters are values used by the function.function functionName (parameters){JavaScript commands}• Convention: place functions in the section of the HTML file or externallyTutorial 10 New Perspectives on HTML and XHTML, 2ComprehensiveXPFunction examplefunction rectArea (width, length){var area = width * length;return area;}var w = 2;var l = 3;var a = rectArea (w, l);Tutorial 10 New Perspectives on HTML and XHTML, 3ComprehensiveXPWorking with Conditional Statements• Conditional statements are commands that run only when specific conditions are met.• Conditional statements require a Boolean expression.• Q: What is a Boolean expression?• you need one of the following operators to create a Boolean expression:– Comparison operators (==, !=, >, <, >=, <=)– Logical operators (&& = AND, || = OR, | = NOT)– Conditional operators – (condition) ? value1:value2Tutorial 10 New Perspectives on ...
Declare a variable Give example of a Numeric, String, Boolean Variable – Assign a value to it What does this do: x = x+1 What does this do: x++ What does this code do: <script type="text/javascript"> var age; age = window.prompt("Please enter your age: ",""); document.write( age + “ is a great age!”); </script>
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
XP
1
XP Creating JavaScript Functions
A function is a series of commands that perform an action or calculates a value. A function name identifies a function. Parameters are values used by the function. function functionName (parameters) { JavaScript commands } Convention: place functions in the <head> section of the HTML file or externally
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
2
Function example
function rectArea (width, length) {
var area width * length; = return area;
} var w = 2; var l = 3; var a = rectArea (w, l);
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
XP
3
Working with Conditional Statements
XP
Conditional statements are commands that run only when specific conditions are met. Conditional statements require a Boolean expression . Q: What is a Boolean expression? you need one of the following operators to create a Boolean expression: – Comparison operators (==, !=, >, <, >=, <=) – Logical operators (&& = AND, || = OR, | = NOT) – Conditional operators – (condition) ? value1:value2
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
4
Comparison Operator
Examples of Boolean expressions using comparison operator – X < 100 – Y == 20 (Note that == is equality operator = is assignment operator) – Y <= 10
Conditional statements are commands that run only when specific conditions are met. – If statement – If-else statement – Switch Statement
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
XP
7
If Statement
if (condition) { Javascript Commands, that execute only if condition is true
} if (day == “Friday”) { document.write(“The weekend is almost here”);
}
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
XP
8
if-else statements
if (condition) { JavaScript commands that execute if condition is true } else { JavaScript commands that execute if condition is false }
if (day == “Friday”) { document.write(“The weekend is almost here!”); } else { document.write(“It’s not yet Friday”); }
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
XP
9
switch statement
Similar to a set of if-else statement Read about it on pages 551/552
Tutorial 10
New Perspectives on HTML and XHTML, Comprehensive
XP
10
Events
Events are actions that can be detected by JavaScript Examples: – A mouse click – A mouse double-click – A web page loading – Mousing over a hot spot on the web page – Selecting an input box in an HTML form – Submitting an HTML form – A keystroke Complete JavaScript Event Reference page: http://www.w3schools.com/jsref/jsref_events.asp