Java Script Programming
Review: Event Handling Text Box Title: Button
JavaScript Adds programming to web pages Performing calculations –Totaling the price or computing sales tax Verifying data –Credit cards Adapting the display to user needs
Basic JavaScript Everything inside is a program We can use variables and expressions –Pay = payRate*hours We can write and reuse our own functions
What are the variable names? HTML Form Widgets Demo Title: US Citizen: Freshman Sophomore Junior Senior Type your address here. red yellow
What are the variable names? HTML Form Widgets Demo Title: US Citizen: Freshman Sophomore Junior Senior Type your address here. red yellow All the same name
Functions A function is a piece of computation that takes 1 or more arguments (data) and returns a value In Javascript, you can make functions yourself, and there are also predefined functions.
Creating your functions Tell the browser that you are going to write code. Use the tag
Functions To create a function function Name( parameterName,... ) {any JavaScript Code } To call a function –function Name(argument expression,... ) –Each argument is assigned to each parameter –The body { } of the function is executed.
An example function function pay(payRate,hours) { return payRate*hours; }
An example function function pay(payRate,hours) { return payRate*hours; } Variables
Function Demo function sayHello() { alert("Hello " + demoform.title.value) }
Putting Strings Together (concatenation) Use the “+” sign to put strings together. Demo
Functions with If and Else function evaluatePay() { if (demoform.totalPay.value < 100) { alert("You earn less than 100 dollars") } else { alert("You earn more than 100 dollars.") }
Functions with If and Else Demo