Saturday 8 January 2011

JavaScript Functions and Definitions

JavaScript Objects Reference
The references describe the properties and methods of each object, along with examples.
• Array object
• Boolean object
• Date object
• Math object
• Number object
• String object
• RegExp object
• Global properties and functions
________________________________________
Browser Objects Reference
The references describe the properties and methods of each object, along with examples.
• Window object
• Navigator object
• Screen object
• History object
• Location object
________________________________________
HTML DOM Objects Reference
The references describe the properties and methods of each object, along with examples.
• Document object
• Event object
• HTMLElement object
• Anchor object
• Area object
• Base object
• Body object
• Button object
• Form object
• Frame/IFrame object
• Frameset object
• Image object
• Input Button object
• Input Checkbox object
• Input File object
• Input Hidden object
• Input Password object
• Input Radio object
• Input Reset object
• Input Submit object
• Input Text object
• Link object
• Meta object
• Object object
• Option object
• Select object
• Style object
• Table object
• TableCell object
• TableRow object
• Textarea object
• JavaScript Array Object

• Array Object
• The Array object is used to store multiple values in a single variable.
• For a tutorial about Arrays, read our JavaScript Array Object tutorial.
• ________________________________________
• Array Object Properties
Property Description
constructor
Returns the function that created the Array object's prototype
length
Sets or returns the number of elements in an array
prototype
Allows you to add properties and methods to an object
• Array Object Methods
Method Description
concat()
Joins two or more arrays, and returns a copy of the joined arrays
join()
Joins all elements of an array into a string
pop()
Removes the last element of an array, and returns that element
push()
Adds new elements to the end of an array, and returns the new length
reverse()
Reverses the order of the elements in an array
shift()
Removes the first element of an array, and returns that element
slice()
Selects a part of an array, and returns the new array
sort()
Sorts the elements of an array
splice()
Adds/Removes elements from an array
toString()
Converts an array to a string, and returns the result
unshift()
Adds new elements to the beginning of an array, and returns the new length
valueOf()
Returns the primitive value of an array

JavaScript Boolean Object
Boolean Object
The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false).
For a tutorial about the Boolean object, read our JavaScript Boolean Object tutorial.
________________________________________
Boolean Object Properties
Property Description
constructor
Returns the function that created the Boolean object's prototype
prototype
Allows you to add properties and methods to an object
Boolean Object Methods
Method Description
toString()
Converts a Boolean value to a string, and returns the result
valueOf()
Returns the primitive value of a Boolean object

JavaScript Date Object
Date Object
The Date object is used to work with dates and times.
Date objects are created with new Date().
There are four ways of instantiating a date:
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
For a tutorial about date and times, read our JavaScript Date Object tutorial.
________________________________________
Date Object Properties
Property Description
constructor
Returns the function that created the Date object's prototype
prototype
Allows you to add properties and methods to an object
Date Object Methods
Method Description
getDate()
Returns the day of the month (from 1-31)
getDay()
Returns the day of the week (from 0-6)
getFullYear()
Returns the year (four digits)
getHours()
Returns the hour (from 0-23)
getMilliseconds()
Returns the milliseconds (from 0-999)
getMinutes()
Returns the minutes (from 0-59)
getMonth()
Returns the month (from 0-11)
getSeconds()
Returns the seconds (from 0-59)
getTime()
Returns the number of milliseconds since midnight Jan 1, 1970
getTimezoneOffset()
Returns the time difference between GMT and local time, in minutes
getUTCDate()
Returns the day of the month, according to universal time (from 1-31)
getUTCDay()
Returns the day of the week, according to universal time (from 0-6)
getUTCFullYear()
Returns the year, according to universal time (four digits)
getUTCHours()
Returns the hour, according to universal time (from 0-23)
getUTCMilliseconds()
Returns the milliseconds, according to universal time (from 0-999)
getUTCMinutes()
Returns the minutes, according to universal time (from 0-59)
getUTCMonth()
Returns the month, according to universal time (from 0-11)
getUTCSeconds()
Returns the seconds, according to universal time (from 0-59)
getYear() Deprecated. Use the getFullYear() method instead
parse()
Parses a date string and returns the number of milliseconds since midnight of January 1, 1970
setDate()
Sets the day of the month (from 1-31)
setFullYear()
Sets the year (four digits)
setHours()
Sets the hour (from 0-23)
setMilliseconds()
Sets the milliseconds (from 0-999)
setMinutes()
Set the minutes (from 0-59)
setMonth()
Sets the month (from 0-11)
setSeconds()
Sets the seconds (from 0-59)
setTime()
Sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970
setUTCDate()
Sets the day of the month, according to universal time (from 1-31)
setUTCFullYear()
Sets the year, according to universal time (four digits)
setUTCHours()
Sets the hour, according to universal time (from 0-23)
setUTCMilliseconds()
Sets the milliseconds, according to universal time (from 0-999)
setUTCMinutes()
Set the minutes, according to universal time (from 0-59)
setUTCMonth()
Sets the month, according to universal time (from 0-11)
setUTCSeconds()
Set the seconds, according to universal time (from 0-59)
setYear() Deprecated. Use the setFullYear() method instead
toDateString()
Converts the date portion of a Date object into a readable string
toGMTString() Deprecated. Use the toUTCString() method instead
toLocaleDateString()
Returns the date portion of a Date object as a string, using locale conventions
toLocaleTimeString()
Returns the time portion of a Date object as a string, using locale conventions
toLocaleString()
Converts a Date object to a string, using locale conventions
toString()
Converts a Date object to a string
toTimeString()
Converts the time portion of a Date object to a string
toUTCString()
Converts a Date object to a string, according to universal time
UTC()
Returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time
valueOf()
Returns the primitive value of a Date object

JavaScript Math Object
Math Object
The Math object allows you to perform mathematical tasks.
Math is not a constructor. All properties/methods of Math can be called by using Math as an object, without creating it.
Syntax
var x = Math.PI; // Returns PI
var y = Math.sqrt(16); // Returns the square root of 16
For a tutorial about the Math object, read our JavaScript Math Object tutorial.
________________________________________
Math Object Properties
Property Description
E
Returns Euler's number (approx. 2.718)
LN2
Returns the natural logarithm of 2 (approx. 0.693)
LN10
Returns the natural logarithm of 10 (approx. 2.302)
LOG2E
Returns the base-2 logarithm of E (approx. 1.442)
LOG10E
Returns the base-10 logarithm of E (approx. 0.434)
PI
Returns PI (approx. 3.14159)
SQRT1_2
Returns the square root of 1/2 (approx. 0.707)
SQRT2
Returns the square root of 2 (approx. 1.414)
Math Object Methods
Method Description
abs(x)
Returns the absolute value of x
acos(x)
Returns the arccosine of x, in radians
asin(x)
Returns the arcsine of x, in radians
atan(x)
Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y,x)
Returns the arctangent of the quotient of its arguments
ceil(x)
Returns x, rounded upwards to the nearest integer
cos(x)
Returns the cosine of x (x is in radians)
exp(x)
Returns the value of Ex
floor(x)
Returns x, rounded downwards to the nearest integer
log(x)
Returns the natural logarithm (base E) of x
max(x,y,z,...,n)
Returns the number with the highest value
min(x,y,z,...,n)
Returns the number with the lowest value
pow(x,y)
Returns the value of x to the power of y
random()
Returns a random number between 0 and 1
round(x)
Rounds x to the nearest integer
sin(x)
Returns the sine of x (x is in radians)
sqrt(x)
Returns the square root of x
tan(x)
Returns the tangent of an angle
JavaScript Number Object
Number Object
The Number object is an object wrapper for primitive numeric values.
Number objects are created with new Number().
Syntax
var num = new Number(value);
Note: If the value parameter cannot be converted into a number, it returns NaN (Not-a-Number).
________________________________________

Number Object Properties
Property Description
constructor
Returns the function that created the Number object's prototype
MAX_VALUE
Returns the largest number possible in JavaScript
MIN_VALUE
Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY
Represents negative infinity (returned on overflow)
POSITIVE_INFINITY
Represents infinity (returned on overflow)
prototype
Allows you to add properties and methods to an object
Number Object Methods
Method Description
toExponential(x)
Converts a number into an exponential notation
toFixed(x)
Formats a number with x numbers of digits after the decimal point
toPrecision(x)
Formats a number to x length
toString()
Converts a Number object to a string
valueOf()
Returns the primitive value of a Number object
JavaScript String Object________________________________________
String Object
The String object is used to manipulate a stored piece of text.
String objects are created with new String().
Syntax
var txt = new String(string);
or more simply:
var txt = string;
For a tutorial about the String object, read our JavaScript String Object tutorial.
________________________________________
String Object Properties
Property Description
constructor
Returns the function that created the String object's prototype
length
Returns the length of a string
prototype
Allows you to add properties and methods to an object
String Object Methods
Method Description
charAt()
Returns the character at the specified index
charCodeAt()
Returns the Unicode of the character at the specified index
concat()
Joins two or more strings, and returns a copy of the joined strings
fromCharCode()
Converts Unicode values to characters
indexOf()
Returns the position of the first found occurrence of a specified value in a string
lastIndexOf()
Returns the position of the last found occurrence of a specified value in a string
match()
Searches for a match between a regular expression and a string, and returns the matches
replace()
Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
search()
Searches for a match between a regular expression and a string, and returns the position of the match
slice()
Extracts a part of a string and returns a new string
split()
Splits a string into an array of substrings
substr()
Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
substring()
Extracts the characters from a string, between two specified indices
toLowerCase()
Converts a string to lowercase letters
toUpperCase()
Converts a string to uppercase letters
valueOf()
Returns the primitive value of a String object
String HTML Wrapper Methods
The HTML wrapper methods return the string wrapped inside the appropriate HTML tag.
Method Description
anchor()
Creates an anchor
big()
Displays a string using a big font
blink()
Displays a blinking string
bold()
Displays a string in bold
fixed()
Displays a string using a fixed-pitch font
fontcolor()
Displays a string using a specified color
fontsize()
Displays a string using a specified size
italics()
Displays a string in italic
link()
Displays a string as a hyperlink
small()
Displays a string using a small font
strike()
Displays a string with a strikethrough
sub()
Displays a string as subscript text
sup()
Displays a string as superscript text
JavaScript RegExp Object

RegExp Object
A regular expression is an object that describes a pattern of characters.
Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text.
Syntax
var txt=new RegExp(pattern,modifiers);

or more simply:

var txt=/pattern/modifiers;
• pattern specifies the pattern of an expression
• modifiers specify if a search should be global, case-sensitive, etc.
For a tutorial about the RegExp object, read our JavaScript RegExp Object tutorial.
________________________________________
Modifiers
Modifiers are used to perform case-insensitive and global searches:
Modifier Description
i
Perform case-insensitive matching
g
Perform a global match (find all matches rather than stopping after the first match)
m Perform multiline matching
Brackets
Brackets are used to find a range of characters:
Expression Description
[abc]
Find any character between the brackets
[^abc]
Find any character not between the brackets
[0-9] Find any digit from 0 to 9
[a-z] Find any character from lowercase a to lowercase z
[A-Z] Find any character from uppercase A to uppercase Z
[a-Z] Find any character from lowercase a to uppercase Z
[adgk] Find any character in the given set
[^adgk] Find any character outside the given set
[red|blue|green] Find any of the alternatives specified
Metacharacters
Metacharacters are characters with a special meaning:
Metacharacter Description
.
Find a single character, except newline or line terminator
\w
Find a word character
\W
Find a non-word character
\d
Find a digit
\D
Find a non-digit character
\s
Find a whitespace character
\S
Find a non-whitespace character
\b
Find a match at the beginning/end of a word
\B
Find a match not at the beginning/end of a word
\0 Find a NUL character
\n
Find a new line character
\f Find a form feed character
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx
Find the character specified by an octal number xxx
\xdd
Find the character specified by a hexadecimal number dd
\uxxxx
Find the Unicode character specified by a hexadecimal number xxxx
Quantifiers
Quantifier Description
n+
Matches any string that contains at least one n
n*
Matches any string that contains zero or more occurrences of n
n?
Matches any string that contains zero or one occurrences of n
n{X}
Matches any string that contains a sequence of X n's
n{X,Y}
Matches any string that contains a sequence of X or Y n's
n{X,}
Matches any string that contains a sequence of at least X n's
n$
Matches any string with n at the end of it
^n
Matches any string with n at the beginning of it
?=n
Matches any string that is followed by a specific string n
?!n
Matches any string that is not followed by a specific string n
RegExp Object Properties
Property Description
global
Specifies if the "g" modifier is set
ignoreCase
Specifies if the "i" modifier is set
lastIndex
The index at which to start the next match
multiline
Specifies if the "m" modifier is set
source
The text of the RegExp pattern
RegExp Object Methods
Method Description
compile()
Compiles a regular expression
exec()
Tests for a match in a string. Returns the first match
test()
Tests for a match in a string. Returns true or false

JavaScript Global
The JavaScript global properties and functions can be used with all the built-in JavaScript objects.
JavaScript Global Properties
Property Description
Infinity
A numeric value that represents positive/negative infinity
NaN
"Not-a-Number" value
undefined
Indicates that a variable has not been assigned a value
JavaScript Global Functions
Function Description
decodeURI()
Decodes a URI
decodeURIComponent()
Decodes a URI component
encodeURI()
Encodes a URI
encodeURIComponent()
Encodes a URI component
escape()
Encodes a string
eval()
Evaluates a string and executes it as if it was script code
isFinite()
Determines whether a value is a finite, legal number
isNaN()
Determines whether a value is an illegal number
Number()
Converts an object's value to a number
parseFloat()
Parses a string and returns a floating point number
parseInt()
Parses a string and returns an integer
String()
Converts an object's value to a string
unescape()
Decodes an encoded string

The Window Object
Window Object
The window object represents an open window in a browser.
If a document contain frames ( or