JavaScript Quick Reference
Language Essentials for JavaScript and HTML Forms
NOTE: This is a selected list, see Netscape's JavaScript Guide for a complete list. The functions listed here should all work in Netscape Navigator 2 and higher (usually equiavalent to Internet Explorer 3 and higher), unless otherwise noted.
alert()
Built-in JavaScript function to display alert dialog box.
alert("Show this alert message")
button
HTML form element, to create a button, typically with an onClick handler to run a JavaScript function; must be enclosed within form tags.
checkbox
comments
Comments in JavaScript are introduced with a double slash (//) at the beginning of the line.
// This is a comment line.
confirm()
Built-in JavaScript function to display confirmation dialog box, asking the user to decide between two options.
if (confirm("Can I continue?")) {
alert ("Thanks, let's move on!");
} else {
alert ("OK, I'll just sit here and twiddle my thumbs.");
}
focus()
Built-in JavaScript function to make targeted object active, as to put cursor in text field.
document.myform.myfield.focus()
form
HTML tag for creating Web form. There must be a opening and closing form tag. Forms may be given names. An "action" handler is added to the opening form tag if a CGI script will be processing the form (i.e. action= "http://www.mysite.com/cgi-bin/myscript.cgi"). To reference a form, use either its name (document.myform) or its array number (document.forms[0] for the first form on the page). See the items for separate form elements: pull-down menu, text field, radio button, checkbox.
<form name=myform>
(All the form elements defined here)
</form>
frames
You can target frames by referencing back to the "parent" document, which is the file containing the frameset definition. The frame can be referenced by its number (starting with 0 and counting down from the frames defined in the frameset document) or by its name (if it's been given one in the frameset document). Use the clear() command (sometimes in combination with the open() command) to begin writing to a frame; end writing to a frame with the close() command (i.e. document.frames[0].close()).
var getframefieldvalue = parent.frames[1].document.myform.myfield.value
function
The definition of an action or series of actions to be carried out, like a sub-subroutine; must be followed by parenthesis and definition enlclosed in curly brackets.
function myfunction() {
alert("Show this alert message")
}
if...then
Control structure for creating branching of actions depending on user input or choice. The condition for determining the result must follow if and be enclosed in parentheses. Action to be carried out is defined in curly brackets. Multiple if statements may follow the initial if through the use of else if. If may also be used by itself. Note that the test for equality in JavaScript is a double equal sign (==); does not equal is !=. With functions which return a value of true or false (as in the confirm() function), no equalizing symbol (==) is needed (see confirm() example). Multiple conditions may follow the initial condition (all in the same parentheses) if separated by a double pipe (||) which is equivalent to "or"; to signify "and" (i.e. more than one condition must be fullfilled), use a double ampersand (&&).
if (myvariable == "right input") {
alert("Right!");
} else {
alert("Wrong!");
}
images
Pre-loading images works only in Netscape 3 and above. This is usually used in combination with image swapping (as in a menu or table of contents).
myimage = new Image()
myimage.src = "me.gif";
indexOf()
Used to search for presence and location of sub-string within a text string. Returns a number corresponding to the number of characters from the beginning of the string. If the searched for sub-string is not found, -1 is returned as a value. LastIndexof() searches backwards for a sub-string.
mystring = "Some text to try out"
test = mystring.indexOf("try")
This returns a value of 13.
onLoad
Used most often to have JavaScript functions run when Web pages is loaded, usually place within the opening body tag. You can also used onUnload to run JavaScript when the user leaves the current page.
<body onLoad=myfunction ()>
onMouseover
Built-in JavaScript function, usually used to run JavaScript functions (as in image swapping) when cursor is moved over a link or an image. To run functions when the cursor leaves the link or image, use onMouseout.
<a href = link.html onMouseover=myfunction()>
prompt()
Built-in JavaScript function to display prompt dialog box, asking the user for input.
userName = prompt ("What is your name?", "Name goes here");
pull-down menu
radio buttons
select()
Built-in JavaScript function to select targeted object (in text field highlights text).
document.myform.myfield.select()
strings
Text strings are enclosed in single or double quotes. To mix and match strings and variables, use the + symbol.
var newvariable = "This is a text string" + myvariable
submit button
Used to submit a form, usually to be processed by a CGI script. An onSubmit handler can be included within the opening form tag, which can then run a JavaScript function first when a form is submitted (an onClick handler could also be put within the submit tag). That function should return a value of true (form is submitted) or false (form is not submitted).
<input type=submit value="Submit this form" onClick="myfunction()">
text area
text field
variables
Defined in JavaScript with the = sign, often preceded by var but that is not required. To mix and match strings and variables, use the + symbol.
var myvariable = "This is some text"
windows
To open a new window, use the window.open() command, as in the example below, which opens a new window 150 by 150 pixels in size and displays the file "hello.html". The win variable is created which can be used to target the window. If you want to target the orginating page from the newly opened window (here from the "win" window), use "win.opener" (assuming "win" as the name of the object which is the newly created window (as here). In addition to width and height the following window characteristics can also be set: 'resizable=yes,toolbar=yes,menubar=yes,location=yes,scrollbars=yes' (each separated by a comma with valid values being yes or no).
win = window.open("hello.html","","width=150,height=150");
write()
Used to write to a document, useful for dynamically building parts of a Web pages. To right a line with a line break at the end, use writeln().
document.write("Adding some text and the value from " + myvariable)
ITA Resources for JavaScript
JavaScript => Index / Presentation / Code / Guide // AIA Portal
Bob Godwin-Jones