JavaScript
JavaScript needs to be “called” Need an event to change things Mouse actions onclick onmouseover onmouseout Other events: when a new page is loaded onload
onload Using JavaScript to create content No user input Comes up when page LOADS <body onload=“doitnow();”>
Calling JavaScript Inside of HTML Statement can be On □ □ □ □ = “JavaScript stmt;” Statement can be assignment statement function call
function Collection of instructions Can be called many times Called as a single statement
JavaScript function Set of instructions Followed step by step in order Statement types: Variable declarations Assignment statements Functions calls Conditional statements
Declared Variables Explicitly define them Can give them starting values JavaScript figures out data type
Data Types Strings Literals can be in ‘’ or “” Set of functions that work on them Numbers Own set of functions, including Math functions Booleans True or false Own set of operators
Assignment Statements Give a value to Value can be Variable Form field HTML element Variables Literals Operators Functions Form fields HTML elements
Conditional Statements (1 of 3) Execute statements if the condition is true if (score==10) { returnmsg = "Awesome! "; } Execute one of 2 groups of statement depending On whether true or false if (score==10) { returnmsg = "Awesome! "; } else { returnmsg = "Keep trying! ";
Conditional Statements (2 of 3) Execute group of statements depending on which condition is true if (num < 60) { grade = "F"; } else if (num < 70) { grade = "D"; } else if (num < 80) { grade = "C"; } else if (num < 90) { grade = "B"; } else { grade = "A"; }
Conditional Statements (3 of 3) Execute each statement in order Can execute all if’s if (weather == "Sunny") { text = text + weather; } if (day == 0) { text = text + " Sunday"; text = text + " is "; if (temp < 60) { text = text + "cool"; else { text = text + "nice";