Javascript Reference Card Page 5

ADVERTISEMENT

FUNCTIONS
PUTTING IT TOGETHER:
JAVASCRIPT AND HTML
Definition: A pre-written block of code that performs a
ON THE WEB
specific task. Some functions return values; others perform
a task like sorting, but return no value. Function names
Cookies: Text-file messages stored by the browser on the
follow the same rules as variables names. There may or
user’ s computer
may not be an argument or parameter in the parenthesis,
Purpose: To identify the user, store preferences, and present
but the parenthesis has to be there.
customized information each time the user visits the page
User-defined Functions:
Types:
Example:
Temporary (transient, session) — stored in temporary
ParseInt() or ParseFloat() convert a string to a
memory and available only during active browser session
number.
Persistent (permanent, stored) — remain on user’ s comput-
To create a function:
er until deleted or expired
Format:
Browser Detection: A script written to determine which
function name_of_function (arguments) {
browser is running; determine if the browser has the capabili-
statements to execute when
ties to load the webpage and support the javascript code; and,
function is called;
if needed, load alternate javascript code to match the browser
}
and platform.
Example:
Sniffing: A script written to determine whether a specific
function kilosToPounds (){
browser feature is present; i.e., detecting the presence of
pounds=kilos*2.2046;
Flash before loading a webpage.
}
Event Handling: Use HTML event attributes (mouseover,
This new function takes the value of the variable kilos,
mouse click, etc.) and connect event to a JavaScript function
multiplies it by 2.2046, and assigns the result to the vari-
called an event handler
able pounds.
To call a function: Give the name of the function followed
by its arguments, if any
ParseInt(X); converts the data stored in the variable
X into a numeric value.
kilosToPounds(17); converts 17 kilos to the same
mass in pounds, returning the value 37.4782.
METHODS
Definition: A special kind of function used to describe or
instruct the way the object behaves. Each object type in
JavaScript has associated methods available.
Examples:
array.sort();
document.write();
string.length();
Calling: To call or use a method, state the method name
followed by its parameters in parentheses.
Example:
document.write(“Hello, world!”);
5

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 6