Docstoc

Javascript Reference

Document Sample
Javascript Reference Powered By Docstoc
					                                Brief JavaScript Reference (TBE 540)

Start/End JavaScript
<script Language=”JavaScript”>
functions, etc.
</script>

Functions (little programs)
Functions are small program that are “called” (started) by some event (like clicking a button or an image).
A function can also be called from another function. The parentheses () are always used after the function
name. Sometimes a value or a variable is placed inside the parentheses to “pass it” on to another function.
function nameOfFunction()
{
instructions here
}
Examples of calling a function:
<input type=”button” value=”Send Number” onClick=”sendOne(Num)”>
<img src=”BIG.gif” onClick=”goThere()”>
if (X<=7) {small()}

Variables
var variableName
var variableName = initial value
If the variable is declared outside any functions, it is global (works in all functions)
If the variable is declared inside a function, it is local (only works in that function)

ALERT and PROMPT Boxes
These commands are used to display information on the screen. ALERT boxes show a message.
PROMPT boxes show a message and wait for input. Examples:
ALERT(“HI THERE!”)
PROMPT(“What is your name?”,”X”) Note: The “” after the comma allow you
                                        to put in a “default” value.

FORMs (buttons , boxes, menus)
FORMs are another way to display information and get input. Instead of floating on top of the page (like
ALERT), they are part of the page. The most commonly used FORM elements are buttons and text boxes.
Example (makes a CLICK HERE button and a place for a name that will hold 20 characters):
<FORM>
<input type=”button” value=”CLICK HERE” onClick=”sendName()”>                    CLICK HERE
<input type=”text” size=20 name=”username”>
</FORM>

There are other types of FORM elements that might be useful. Radio buttons are small, round buttons
while checkboxes are square . Both are used to get input (usually from a list of choices). Radio buttons
are generally set up so that the user can only choose one. They are good for multiple choice tests.
Checkboxes are usually used when the user can make more than one choice. Examples (assuming there
was a <FORM> tag somewhere above the command):
<INPUT type="checkbox" name="Favorite_Food" value="Big Mac">Big Mac
<INPUT type="radio" name="Favorite_Color" value="Rainbow">Rainbow
Some web pages use “pull-down” menus like this:                    The code looks like this:
 <SELECT name="Favorite_Computer">
 <OPTION SELECTED>Mac
 <OPTION>IBM
 <OPTION>Apple II
 </SELECT>
Menus are also useful for multiple choice tests or surveys.

“Textareas” are larger boxes for text. An example (5 rows, 30 columns):
<TEXTAREA name="message" rows=”5” cols=”30”></TEXTAREA>

Loops (Repetitions)
Loops repeat a series of commands a given number of times (“for” loops) or until a given condition is
true (“while” loops). Loops use a variable that is a “counter” (keeps track of how many times the loop has
repeated.). Braces ({}) are used to show which commands are repeated. Examples:
for (var j=0; j<10; j++) Use variable j as a counter, start at 0 (j=0), keep going while j is
{                            less than 10 (j<10), add 1 to j each repetition (j++)
repeated statements
}
while (var k++<140)         Use variable k, add 1 to k each repetition (k++), keep going
{                           while K is less than 140)
repeated statements
}

IF Statements (decisions)
Statements containing if are used when you want the program to make a decision based on a “condition”
(often whether a variable is equal to a set value). There are several possibilities. (1) If the condition is
true, perform a set of instructions; otherwise, go to the next instruction. (2) If the condition is true,
perform a set of instructions; otherwise perform a different set of instructions. Braces ({}) are used to
show which instructions should be done. Note: “==” means “exactly equal to”. Examples:
if (Age>50) {alert(“YOU ARE OLD!”)}                 If the value of the variable Age is greater
                                                    than 50, display YOU ARE OLD! If not,
                                                    just go to the next line.
If (Ans==”TRUE”)                                    If the variable Ans is equal to the word TRUE,
{alert(“RIGHT”)}                                    display RIGHT
else                                                otherwise
{alert(“WRONG”)}                                    display WRONG.

Calculations
Calculations use these symbols: + (add) - (subtract) * (multiply) / (divide) ^ (power)
A calculation usually begins with a variable name: var X = 100 * testScore There are also shortcuts,
such as var p++ (++ means “add 1 to this variable”).

Random Numbers
Random numbers use built-in “Math” functions. For example, the code get a random number from 0 to 10
is Math.floor(Math.random()*11) Math.random() gives a decimal number between 0 and 0.999999.
Multiplying it by 11 gives a decimal number from 0 to 10.999999. Math.floor() eliminates the decimal
part, making it a whole number between 0 and 10. If you want to round to the nearest whole number, use
Math.round()

				
DOCUMENT INFO
Description: This is an example of javascript reference. This document is useful for studying javascripts.