A Summary of JavaScript Operations | |
|---|---|
| Computational Operators | |
| + | Addition, String Concatenation |
| - | Subtraction, Unary Negation |
| * | Multiplication |
| / | Division |
| % | Modulus |
| ++ | Preincrement, Postincrement |
| -- | Predecrement, Postdecrement |
| Logical Operators | |
| ==, != | Equality, Inequality |
| <,<=, =>, > | Arithmetic and String Comparison |
| ! | Logical NOT |
| &&, || | Logical AND, Logical OR |
| ? | Conditional Selection (trinary) |
| , | Logical Concatenation |
| Bitwise Operators | |
| &, | | Bitwise AND, Bitwise OR |
| ^ | Bitwise eXclusive OR (XOR) |
| ~ | Bitwise NOT |
| <<, >>, >>> | Shift Left, Shift Right, Unsigned Shift Right |
| Assignment Operators | |
| = | Assignment |
| OP= | Aggregate Assignment (+, -, *, /, %, &, |, ^, ~, <<, >>, >>>) |
JavaScript's Bitwise Operators | |
|---|---|
| Operator Name | Symbol |
| Bitwise AND | & |
| Bitwise OR | | |
| Bitwise XOR | ^ |
| Bitwise Left Shift | << |
| Bitwise Signed Right Shift | >> |
| Bitwise Unsigned Right Shift | >>> |
| Bitwise NOT | ~ |
The following methods can be used on string objects to access, control, or modify their content:
The string appearance methods are as follows:
The various methods that you can call with the string object to alter its HTML formatting.
String Object Methods for HTML Formatting | ||
|---|---|---|
| Method Name | Example | Returned Value |
| anchor | "foo".anchor("anchortext") | <A NAME="anchortext">foo</A> |
| big | "foo".big() | <BIG>foo</BIG> |
| blink | "foo".blink() | <BLINK>foo</BLINK> |
| bold | "foo".bold() | <B>foo</B> |
| fixed | "foo".fixed() | <TT>foo</TT> |
| fontcolor | "foo".fontcolor("green") | <FONT COLOR="green">foo</FONT> |
| fontsize | "foo".fontsize(-1) | <FONT SIZE="-1">foo</FONT> |
| italics | "foo".italics() | <I>foo</I> |
| link | "foo".link("linktext") | <A HREF="linktext">foo</A> |
| small | "foo".small() | <SMALL>foo</SMALL> |
| strike | "foo".strike() | <STRIKE>foo</STRIKE> |
| sub | "foo".sub() | <SUB>foo</SUB> |
| sup | "foo".sup() | <SUP>foo</SUP> |
| toLowerCase | "UPPERcase".toLowerCase() | uppercase |
| toUpperCase | "UPPERcase".toUpperCase() | UPPERCASE |
String Object Methods for Displaying Subsets of Strings | ||
|---|---|---|
| Method Name | Example(s) | Returned Value |
| charAt | "netscape navigator".charAt(0) | n |
| "netscape navigator".charAt(10) | a | |
| indexOf | "netscape navigator".indexOf("scape") | 3 |
| "netscape navigator".indexOf("n",2) | 9 | |
| lastIndexOf | "netscape navigator".lastIndexOf("a") | 14 |
| "netscape navigator".lastIndexOf("a", 12) | 10 | |
| substring | "netscape navigator".substring(0,7) | netscap |
| "netscape navigator".substring(7,0) | netscap | |
| "netscape navigator".substring(0,50) | netscape navigator | |
| length | "netscape navigator".length | 18 |
| "netscape navigator".substring(0,7).length | 7 | |
The Math object has the following properties:
The Math object has the following methods:
Math Object Methods and Properties | ||
|---|---|---|
| Method Name | Example | Returned Value |
| abs | Math.abs(-79) | 79 |
| acos | Math.acos(.5) | 1.047197551196597631 |
| asin | Math.asin(1) | 1.570796326794896558 |
| atan | Math.atan(.5) | 0.4636476090008060935 |
| ceil | Math.ceil(7.6) | 8 |
| cos | Math.cos(.4) | 0.9210609940028851028 |
| exp | Math.exp(8) | 2980.957987041728302 |
| floor | Math.floor(8.9) | 8 |
| log | Math.log(5) | 1.609437912434100282 |
| max | Math.max(1 , 700) | 700 |
| min | Math .min(1 , 700) | 1 |
| pow | Math.pow(6,2) | 36 |
| random (not fully implemented) | Math.random() | .7877896 |
| round | Math.round(.567) | 1 |
| sin | Math.sin(Math.PI) | 0 |
| sqrt | Math.sqrt(9801) | 99 |
| tan | Math.tan(1.5*Math.PI) | INF (infinity) |
Math Object Properties Summary | ||
|---|---|---|
| Property Name | Example | Returned Value |
| E (2.718281828459045091) | Math.E*5 | 13.59140914229522501 |
| LN10 (2.302585092994045901) | Math.LN10/6 | 0.3837641821656743168 |
| LN2 (0.69314718055994529) | Math.LN2-Math.E | -2.025134647899099694 |
| PI (3.141592653589793116) | Math.sin(2*Math.PI/4) | 0.2741213359 |
| SQRT2 (1.414213562) | 1/Math.SQRT2 | 0.7071067811865474617 |
The Math object provides you with a few constants that you can use for various scientific and algebraic functions. Note that these properties are constants and cannot be changed by JavaScript. The following is a summary with each property's approximate values:
Date object methods
Date Object Methods | ||
|---|---|---|
| Method | Example | Returns |
| getDate | today.getDate() | 5 |
| getDay | yesterday.getDay() | 2 |
| getHours | today.getHours() | 5 |
| getMinutes | today.getMinutes() | 30 |
| getMonth | year.getMonth() | 6 |
| getSeconds | time.getSeconds() | 13 |
| getTime | now.getTime() | ***** |
| getTimeZoneoffset | today.getTimeZoneoffset | ****** |
| getYear | now.getYear | 96 (the years since 1900) |
| parse | Date.parse(July 1, 1996) | ***** |
| setDate | now.setDate(6) | - |
| setHours | now.setHours(14) | - |
| setMinutes | now.setMinutes(50) | - |
| setMonth | today.setMonth(7) | - |
| setSeconds | today.setSeconds(7) | - |
| setTime | today.setTime(yesterday.getTime()) | - |
| setYear | today.setYear(88) | - |
| toGMTString | yesterday.toGMTString() | Sat, Feb 24 1996 14:28:15 GMT |
| toLocaleString | today.toLocaleString() | 2/25/96 14:28:15 |
| UTC | Date.UTC(96, 11,3,0,0,0) | - |
Date Object Number Conventions | |
|---|---|
| Date Attribute | Numeric Range |
| seconds, minutes | 0 - 59 |
| hours | 0 - 23 |
| day | 0 - 6 (0 = Sunday, 1 = Monday, and so on) |
| date | 1 - 31 |
| month | 0 - 11 (0 = January, 1 = February, and so on) |
| year | 0 + number of years since 1900 |
(top)
Built-in functions
The primary browser objects, in rough order of significance, are as follows:
JavaScript maintains an idea of the current window, so that almost all references to sub-objects of the current window do not need to refer to it explicitly. This is why all of our output has been done using document.write() rather than window.document.write(). window objects have the following interesting methods (among others):
The document object has the following methods:
HISTORY
The history object is used to refer to the history list
of previously visited URLs. The history object has a
property known as length, which indicates how many URLs
are stored on the history list at present. It also has the following
three methods:
Quick Review HTML Form Tags | |
|---|---|
| Tag | Meaning |
| <FORM ACTION="URL" METHOD="GET"></FORM> | Defines a form |
| <FORM ENCTYPE="multipart/form-data"></FORM> | Uploads a file via a form |
| <INPUT TYPE="TEXT"> | Input field |
| <INPUT NAME="somename"> | Field name |
| <INPUT VALUE="somevalue"> | Field value |
| <INPUT CHECKED> | Checked |
| <INPUT SIZE=6> | Field size (in characters) |
| <INPUT MAXLENGTH="100"> | Maximum length of field |
| <SELECT></SELECT> | Selection list |
| <SELECT NAME="somename"> | Selection list name |
| <SELECT SIZE="6"> | Number of options in list |
| <SELECT MULTIPLE> | Multiple selections (more than one) |
| <OPTION>sometext | Option |
| <OPTION SELECTED> | Default Option |
| <TEXTAREA ROWS="5" COLS="5">...</TEXTAREA> | Text Area Input Box size |
| <TEXTAREA NAME="somename">...</TEXTAREA> | Text area name |
| <TEXTAREA WRAP=OFF> | Wraps the text |
The button object is a new type of INPUT tag that allows you to create general purpose buttons in a form. You can use these buttons to activate any function, or to open a new URL, or perform any other action that JavaScript can initiate. To define a button object use the following syntax:
<INPUT
TYPE = "button"
NAME =" nameInside"
VALUE = "nameOnButton"
[onClick = "handlerText"]>
This is an extremely useful object, and you will probably find yourself using this often to create buttons to run scripts, open new windows, or even cause the browser to leave your Web site altogether! You must use this inside a set of <FORM>...</FORM> tags. You access a button's properties and methods in the following way:
Text Object Properties | ||
|---|---|---|
| Property | Example | Description |
| defaultValue | myText.defaultValue | The value of the input tag at page load time |
| name | myText.name | The NAME argument |
| value | formName.elements[0].value | The VALUE argument |
You can act on this object in a number of ways, either indirectly
by another function, or directly by using the event handlers contained
in the object.
Text Object Methods | ||
|---|---|---|
| Method | Example | Description |
| focus | myText.focus() | Equivalent to clicking this field |
| blur | myText.blur() | Equivalent to clicking another field after using this one |
| select | myText.select() | Equivalent to dragging the mouse across all the text in this field-selecting it |
Properties and Methods of the Window Object
Properties Description defaultStatus The default message in the status bar. document The current document contained in the window. frames An array that describes all of the frames (if any) in the window. frame A frame object. length Reflects the number of frames (if any) in the window. name The name of the window. parent Synonymous with the name of the window. Contains the frameset tags. self Synonymous with the name of the window and refers to the current window. status Value appears in the window's status bar. Usually only lasts a moment before overwritten by some other event. top Synonymous with the name of the window and represents the topmost window. window Synonymous with the name of the window and refers to the current window. location A string specifying the URL of the current document. Methods
alert Brings up an alert dialog box. close Closes the window. confirm Brings up a dialog box with Yes or No buttons and a user-specified message. open Opens a new window. prompt Brings up a window with user-specified text and an input box that allows the user to type in information. setTimeout Sets a time in milliseconds for an event to occur. clearTimeout Resets value set by setTimeout.
The Document object is extremely useful because it contains so much information about the current document, and it can create HTML on-the-fly with its write and writeln methods. The following table lists the properties and methods of the document object, as well as short descriptions of their purpose.
Properties and Methods of the Document Object
Properties Description alinkColor Reflects the ALINK attribute (in the <body> tag). anchors An array listing all of the HTML anchors in the document (<a name>). anchor An anchor object. bgColor Reflects the value of the BGCOLOR attribute. cookie Reflects the value of a Netscape cookie. fgColor The value of the TEXT attribute (in the <body> tag). forms An array listing all the forms in the document. form A form object. history An object containing the current browser history (links visited, number of links visited, and link URLs). lastModified The date the document was last modified. linkColor Reflects the LINK attribute of the <body> tag. links An array of all HTML links in the document (<a href>). link A link object. location The URL of the document. referrer The URL of the document that called the current document. title Reflects the title of the document. vlinkColor Reflects the color listed in the VLINK attribute. Methods
clear Clears the window of all content. close After an open-causes the string buffer to be written to the screen. open Begins a string to be written to the screen. Needs a close to actually force the writing. write Writes some expression to the current window. writeln Same as write but adds a newline character at the end.
This object is created every time JavaScript encounters a <form>...</form> in your HTML documents. It contains all of the information stored in your form and can be used to submit information to a function or back to the server. This Table describes the properties and methods of the Form object.
Table: Properties and Methods of the Form Object
Property Description action Reflects the HTML ACTION attribute of the <form> tag. button A button object (<input type=button>). checkbox A checkbox object (<input type= checkbox>). elements An array listing all elements in a form. encoding The value of the ENCTYPE attribute (for HTML uploads in Netscape). hidden A hidden object (<input type=hidden>). length The number of elements in the form. method The METHOD attribute of <form>. password A password object (<input type=password>). radio A radio object (<input type=radio>). reset A reset button object. select A select object (<select>...<select>). submit A submit button object. target The TARGET attribute of <form>. text A text object (<input type=text>). textarea A textarea object (<textarea>...</textarea>). Method
submit Submits the form to the location in the ACTION attribute.
The Navigator object is distinct from the window object in that it contains information about the browser that persists across any given window. In Netscape 3.0, JavaScript adds two new properties-an object called mimeTypes, which lists all of the mimeTypes the browser can handle; and plug-ins, which lists all of the registered plug-ins the browser can use. This table summarizes the properties of the Navigator object (it has no associated methods).
Tabl: e Navigator Object Properties
Property Description appCodeName The code name of the browser, such as "Mozilla". appName The name of the browser, such as "Netscape"." appVersion Contains the version information of the browser, such as "2.0 (Win95, I)." userAgent Contains the user-agent header that the browser sends to the server to identify itself, such as "Mozilla/2.0 (Win95, I)." mimeTypes An array reflecting all possible MIME types the browser can either handle itself or pass on to a plug-in or helper application (Netscape 3.0). plug-ins An array of registered plug-ins that the browser currently has loaded.
Other objects are built into JavaScript that are not specific to either the browser or the window. The first of these is the String object. This object is very useful because you can use its methods to modify and add HTML modifications without changing the string itself. One item to notice about this object is that you can string together any number of its methods to create multilayers of HTML encoding. For example,
"Hello!".bold().blink()
would return
<blink><b>Hello!</b></blink>
Table: Properties and Methods of the String Object
Property Description length The number of characters in the string. Methods
anchor Converts string to an HTML anchor. big Encloses string in <big>...</big>. blink Encloses string in <blink>...</blink>. bold Encloses string in <b>...</b>. charAt Returns the character at some index value. Index reads from left to right. If char not found, it returns a -1. fixed Encloses string in <tt>...</tt>. fontcolor Encloses string in <font color=somecolor>...</font>. indexOf Looks for the first instance of some string and returns the index of the first character in the target string, or gives a -1 if not found. italics Encloses string in <i>...</i>. lastIndexOf Same as indexOf, only begins searching from the right to find the last instance of the search string, or -1 if not found. link Converts string into a hyperlink. small Encloses string in <small>...</small>. strike Encloses string in <strike>...</strike>. sub Encloses string in <sub>...</sub>. substring Given a start and end index, returns the string contained by those indices. sup Encloses string in <sup>...</sup>. toLowerCase All uppercase characters are converted to lowercase(UpPeRcAsE becomes uppercase). toUpperCase All lowercase characters are converted to uppercase.
Events and Event Handlers in JavaScript
| Event | Event Handler | To Trigger Event |
| blur | onBlur | In a form element, user clicks (tabs) away from element. |
| click | onClick | In a form element or link, user clicks element. |
| change | onChange | In a form text, text area, or select object, user changes value. |
| focus | onFocus | In a form element, clicks (tabs) to element. |
| load | onLoad | Happens when page is loaded. |
| mouseover | onMouseOver | Happens when mouse is passed over links or anchors. |
| select | onSelect | In a form, user selects input field. |
| submit | onSubmit | In a form, user submits a form (clicks the Submit button). |
| unload | onUnload | User leaves the page. |