Previous Page TOC Next Page



- E -
JavaScript Language Reference


This appendix contains a summary or quick reference for the JavaScript language.

Language keywords and symbols are shown in a monospace font. Arguments and other parts to be substituted are in italic monospace.

Optional parts are indicated by brackets (except in the array syntax section). If several options are mutually exclusive, they are shown separated by pipes (|) like this:




[ public | private | protected ] type varname

Reserved Words


The following words are defined as part of the JavaScript language and cannot be used as variable names:

abstract extends interface synchronized
boolean false long this
break final native throw
byte finally new throws
case float null transient
catch for package true
char function private try
class goto protected var
const if public void
continue implements return while
default import short with
do in static
double instanceof super
else int switch

Literals


number Base 10 Integer (for example, 8)
0number Octal Integer (for example, 034)
0xnumber Hex Integer (for example, 0x3F8)
num1.num2 Floating point (for example, 453.2454)
num1Enum2 Exponential floating point (for example, 24.9E6)
'characters' String
"characters" String
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Tab
\" Double quote
true Boolean
false Boolean

Operators


The JavaScript operators are grouped into the following categories: assignment, comparison, arithmetic, string, logical, and bitwise.

Assignment


variable = value Assignment of value to variable
variable += expression variable = variable + expression
variable -= expression variable = variable - expression
variable *= expression variable = variable * expression
variable /= expression variable = variable / expression
variable %= expression variable = variable % expression
variable <<= expression variable = variable << expression
variable >>= expression variable = variable >> expression
variable >>>= expression variable = variable >>> expression
variable &= expression variable = variable & expression
variable ^= expression variable = variable ^ expression
variable |= expression variable = variable | expression

Comparison


== Equal
!= Not equal
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to

Arithmetic


var1 + var2 Addition
var1 - var2 Subtraction
var1 * var2 Multiplication
var1 / var2 Division
var1 & var2 Modulus
- Negation
var++ Postfix increment
++var Prefix increment
var-- Postfix decrement
--var Prefix decrement

String


string1 + string2 String concatenation

Logical


expr1 && expr2 Logical AND
expr1 || expr2 Logical OR
!expr Logical NOT

Bitwise


arg1 & arg2 Bitwise AND
arg1 | arg2 Bitwise OR
arg1 ^ arg2 Bitwise XOR
arg1 << arg2 Left shift
arg1 >> arg2 Right shift
arg1 >>> arg2 Zero-fill right shift

Statements


JavaScript provides the traditional conditional and loop statements as well as a set of object manipulation statements.

Conditionals





if (condition) {



   // statements for true condition 



}



else {



   // statements for false condition



}

Loops





for (initExpr; condition; incrExpr) {



   // statements to execute while looping



   [break;]



   [continue;]



}



while (condition) {



   // statements to execute while looping



   [break;]



   [continue;]



}

Object Manipulation





for (variable in object) {



   // statements



   }




objectvar = new objecttype ( param1 [, param2] ... [,paramN] ); with(object){ // statements }
this[.property]

JavaScript Object Hierarchy



Navigator Object


The navigator object is used to obtain information about the browser, such as the version number. The navigator object does not have any methods or event handlers associated with it.

Properties

userAgent specifies the user-agent header.

The following example displays each property of the Navigator object:




<HTML><HEAD>



<TITLE>A Navigator Object Example</TITLE>



<SCRIPT LANGUAGE="JavaScript">



document.write("appCodeName = " + navigator.appCodeName+ "<BR>");



document.write("appName = " + navigator.appName + "<BR>");



document.write("appVersion = " + navigator.appVersion + "<BR>");



document.write("userAgent = " + navigator.userAgent + "<BR>");



</SCRIPT>



</HEAD>



<BODY>



</BODY>



</HTML>

Window Object


The window object is a top-level object. The document, frame, and location objects are all properties of the window object.

Properties

window refers to the current window.


Methods

alert("message")

clearTimeout(timeoutID)

windowReference.close

confirm("message")

[windowVar = ][window].open("URL", "windowName", ["windowFeatures"])

prompt("message" [,"defaultInput"])

TimeoutID = setTimeout(expression, msec)

Evaluates expression after msec has elapsed.


Event Handlers

onUnload occurs when a window is unloaded.


Location Object


The location object contains properties with information about the URL of the current document. This object does not have any methods or event handlers. Refer to the following sample URL in the property descriptions that follow:




http://www.abc.com/chap1/page2.html#topic3

Properties

search is a search query that may be at the end of an URL for a CGI script.


Frame Object


A window can have several frames. The frames can scroll independently, and each can have a unique URL. A frame has no event handlers. The onLoad and onUnload events are for the window object.

Properties

window is the current frame.


Methods

clearTimeout(timeoutID)

TimeoutID = setTimeout(expression, msec)

Evaluates expression after msec has elapsed.


Document Object


The document object contains information about the current document and provides methods for writing information to the screen. The document object is created by the <BODY></BODY> tag pair. Several of the properties reflect attributes associated with the <BODY> tag.

The anchor, form, history, and link objects are properties of the document object.

Properties

vlinkColor is the VLINK attribute.


Methods

document.clear

document.close

document.open(["mimeType"])

document.write(expression1 [,expression2], ...[,expressionN])

document.writeln(expression1 [,expression2], ...[,expressionN])

Writes HTML expressions to a document in the specified window and follows them with a newline.


Anchor Object


An anchor is text in a document that can be the target of a hyperlink. The anchor is defined with the <A></A> tag pair. The anchor object has no properties, methods, or event handlers. The anchors array references each named anchor in a document. Anchors are referenced as follows:




document.anchors[index]

The anchors array has a single property, length, which stores the number of named anchors in the document. You can refer to this property as follows:




document.anchors.length

Form Object


Forms are created with the <FORM></FORM> tag pair. Most of the form object's properties reflect attributes of the <FORM> tag. The form object also has several objects as properties:

If a document contains several forms, you can reference them with the forms array. You find the number of forms with the following:




document.forms.length

Each form can be referenced as follows:




document.forms[index]

Properties

target is the TARGET attribute.


Methods

formName.submit()

Submits the form named formName.


Event Handlers

onSubmit occurs when a form is submitted.


History Object


The history object stores information about the previous URLs visited by the user. The list of URLs is stored in chronological order. The history object has no event handlers.

Properties

length is the number of entries in the history object.


Methods

history.back()

history.forward()

history.go(delta | "location")

Used to either move forward or backward delta number of entries on the history list or to go to a specific URL on the history list referred to by location. If delta is used, the reference is backward if negative and forward if positive. If location is used, the closest URL containing location as a substring is called.


Link Object


A link object is text or a picture that is specified as a hyperlink. The properties of the link object deal primarily with the URL of the hyperlink. The link object does not have any methods.

The links array contains a list of all the links in a document. You can find the number of links with the following line:




document.links.length()

A specific link can be referenced by the following array:




document.links[index]

Refer to the following sample URL for the link object's properties descriptions:




http://www.abc.com/chap1/page2.html#topic3

Properties

target is the same as the TARGET attribute of <LINK>.


Event Handlers

onMouseOver occurs when the mouse moves over the link.


Math Object


The math object is built in to JavaScript. This object's properties contain many common mathematical constants. The math methods provide a wide array of trigonometric and other mathematical functions. The math object has no event handlers.

The reference to number in the methods can be either a number or an expression that evaluates to a valid number.

Properties

SQRT2 the square root of 2, approximately 1.414.


Methods

Math.abs(number)

Math.acos(number)

Math.asin(number)

Math.atan(number)

Math.ceil(number)

Math.cos(number)

Math.exp(number)

Math.floor(number)

Math.log(number)

Math.max(num1, num2)

Math.min(num1, num2)

Math.pow(base, exponent)

Math.random()

Math.round(number)

Math.sin(number)

Math.sqrt(number)

Math.tan(number)

Returns the tangent of number.


Date Object


The date object is built in to JavaScript. This object provides many useful methods for getting and dealing with the time and date. The date object has no properties or event handlers associated with it.

Most of the date methods have a date object associated with it. The following method definitions use a date object named dateVar. The examples for many of the methods use the value stored in dateVar as follows:




dateVar = new Date("August 16, 1996 20:45:04");

Methods

dateVar.getDate()

dateVar.getDay()

dateVar.getHours()

dateVar.getMinutes()

dateVar.getMonth()

dateVar.getSeconds()

dateVar.getTime()

dateVar.getTimezoneOffset()

dateVar.getYear()

Date.parse(dateStr)

dateVar.setDate(day)

dateVar.setHours(hours)

dateVar.setMinutes(minutes)

dateVar.setMonth(month)

dateVar.setSeconds(seconds)

dateVar.setTime(value)

dateVar.setYear(year)

dateVar.toGMTString()

dateVar.toLocaleString()

Date.UTC(year, month, day [,hours] [,minutes] [,seconds])

Returns the number of milliseconds since Jan. 1, 1970, 00:00:00 GMT.


String Object


The string object is built in to JavaScript. This object provides many string-manipulation methods. The only property for the string object is the length.

The string object has no event handlers.

Properties

length is the length of the string.


Methods

str.anchor(name)

str.big()

str.blink()

str.bold()

str.charAt

str.fixed()

str.fontcolor(color)

str.fontsize(size)

str.indexOf(srchStr [,index])

str.italics()

str.lastIndexOf(srchStr [, index])

str.link(href)

str.small()

str.strike()

str.sub()

str.substring(a, b)

str.sup()

str.toLowerCase()

str.toUpperCase()

Converts str to uppercase.

Previous Page Page Top TOC Next Page