<!-- SophiaKnows -->

JAVASCRIPT BASICS
Rev 4: December 2004

<  PART:   A · 1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9  >

 

8. Built-In Methods

JavaScript includes a number of built-in methods for inputing and outputing data.

Some of these methods are attached to the lowest level objects window and document including for example the document.write() method which we have seen several times to this point.

Some of these methods, alert(), confirm() and prompt() are not attached to any specific object.

This section formally reviews these I/O methods as well as adding a couple of new tools: window.open() and document.getElementById()

8.1 Generic Methods

JavaScript includes several built-in methods that are not attached to any specific object: alert(), confirm() and prompt()

As was show in several previous examples, these methods are invoked as bareword function names to the following results:

8.1.1 Alert()

The alert() method opens a dialog box including a single [OK] button and prints the contents of a message, [message] designated by the developer:

alert([message]);

The alert() method has no result or return value.

8.1.2 Confirm()

The confirm() method opens a dialog box including a pair of [Cancel] and [OK] buttons and prints the contents of a message, [message] designated by the developer:

confirm([message]);

The confirm() method returns of true or false depending on the dialog button selected by the user: [OK] => true; [Cancel] => false.

8.1.3 Prompt()

The prompt() method opens a dialog box including a pair of [Cancel] and [OK] buttons and a single line text input, and prints the contents of a message, [message] designated by the developer:

prompt([message]);

The prompt() method returns one of several possible values: First, if the user selects [Cancel], the result is false. Second, if the user enters no text in the input but clicks [OK], the result is null and/or false. Third, if the user enters some text in the input and clicks [OK], the result is value of the input contents and/or true.

8.2 Document Methods

8.2.1 Document.Write()

The document.write() method prints the value of [contents] as inline html/text content:

document.write([contents]);

Note that, subject to certain security restrictions, the document can be a document open in a window (or frame) other than the current window (see window.open() below).

8.2.2 Document.Writeln()

The document.writeln() method prints the value of [contents] as inline html/text content followed by a newline (\n | \r) character

document.writeln([contents]);

Note that if the contents of [contents] is HTML this will result in a displayed space as opposed to a hard return (<br />)

8.2.3 Document.Close()

The document.close() method closes the body of the document to further inline input.

document.close();

Subsequent document.write() commands will clear the current displayed contents of the document and begin a fresh <body> element. This behavior is useful primarily when writing to a document open in other than the current window.

8.3 Window Methods

8.3.1 Window Open

window.open([uri],[title],[parameters]);

8.3.2 Window Close

window.close();

 

 

 

 

Part 8 of 9 | Next Part 9: Built-in Arrays >

<  PART:   A · 1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9  >

< CODEBASE | TOP^ | MAINPAGE >

Text & Design By Tony Pisarra
© SophiaKnows 1998-2004