Class 5 Notes for Web Scripting 

Learning Objectives:

You will be able to identify the different events that Javascript uses to launch a function.

You will be able to call a function and return a value.

 

Homework:

Read: Chapter 5

Create: Complex Calculator using functions and Link to Homework page!

Read: Functions at W3schools

Review: For Loop, Switch and While Loop

 

Topics Covered:

Function

function name_of_function(parameter, parameter)

{


Java_script_code_block_using_parameters;

}

 

The code inside the function will execute when "something" invokes (calls) the function

Event
What it does
onclick: JavaScript starts by clicking (a link, or form boxes)
onload: JavaScript starts after the page or an image has finished loading.
onmouseover: JavaScript starts by the mouse moving over a link
onmouseout: JavaScript starts by the mouse moving off the link
onunload: JavaScript starts after you leave the page.

 

When you call a function, you can pass along some values to it, these values are called arguments or parameters (fancy name for a variable.)

These arguments can be used inside the function.

You can send as many arguments as you like, separated by commas (,)

 

 
Functions
Syntax
Example

function name_of_function(parameters
{


Java_script_code_block


}

function sayhi(name )
{
alert(name);
}

Call sayhi(jon )

Alert box with Jon

For Loops 
Syntax
Example
for (start; end; increment) 

Java_script_code 
}
for (i = 0; i <= 5; i++)
{
document.write("The number is " + i + "<br />")
}
While Loops 
Syntax
Example

while (condition_end) 

Java_script_code
increment
}

i = 0
while (i <= 5)
{
document.write("The number is " + i + "<br />");
i++;
}
Do While Loops 
Syntax
Example

do

Java_script_code
increment
}
while (condition_end)

var i=0
do {
document.write("The number is " + i+ "<br />"); 
i=i+1; 
}
while (i <= 5)

Terms:

Variables - Local vs Global

Global: Variables declared outside a function, become Global, and all scripts and functions on the web page can access it.

Local: A variable declared (using var) within a JavaScript function becomes Local and can only be accessed from within that function. (the variable has local scope).Local variables are deleted when the function is completed.

Local variables can be accesed (turned Global) by the rest of the program using the return statement.

 

 

 

HTML Valid