[Chapter 21] Reference: Document
Author: David Flanagan
Date: Fri Aug 29 11:14:51 1997

#

| Chapter 21 JavaScript Reference |

# Document Object

## Name

Document Object---the currently displayed HTML document

## Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

## Synopsis

    window.document
    document

An instance of the Document object is stored in the `document` field of the Window object. As a special case, when referring to the Document object of the current window (i.e., the window in which the JavaScript code is executing), you can omit the `window` reference, and simply use `document`.

## Properties

`alinkColor`

  A string that specifies the color of activated links. May be set in the document `<HEAD>` or through the `ALINK` `<BODY>` attribute.

`anchors[]`

  An array of Anchor objects; one for each hypertext target in the document.

`anchors.length`

  A read-only integer specifying the number of elements in the `anchors[]` array.

`applets[]`

  An array of Java objects, one for each `<APPLET>` that appears in the document. Navigator 3.0 and higher.

`applets.length`

  A read-only integer specifying the number of elements in the `applets[]` array. Navigator 3.0 and higher.

`bgColor`

  A string that specifies the background color of the document. Initially set through the `BGCOLOR <BODY>` attribute.

`cookie`

  A string, which is the value of a cookie associated with this document.

`domain`

  A string that specifies the Internet domain from which the document is from. This property is used for security purposes. Navigator 3.0 and higher.

`embeds[]`

  An array of Java objects, one for each `<EMBED>` tag that appears in the document. Navigator 3.0 and higher.

`embeds.length`

  A read-only integer that specifies the number of elements in the `embeds[]` array. Navigator 3.0 and higher.

`fgColor`

  A string that specifies the color of document text. May be set in the document `<HEAD>` or through the `TEXT <BODY>` attribute.

`forms[]`

  An array of Form objects, one for each `<FORM>` that appears in the document.

`forms.length`

  A read-only integer specifying the number of elements in the `forms[]` array.

`images[]`

  An array of Image objects, one for each image embedded in the document with the `<IMG>` tag. Navigator 3.0 and higher.

`images.length`

  The number of elements in the `images[]` array.

`lastModified`

  A read-only string that specifies the date of the most recent change to the document (as reported by the web server).

`linkColor`

  A string that specifies the color of unvisited links in the document. May be set in the document `<HEAD>` or through the `LINK <BODY>` attribute.

`links[]`

  An array of Link objects, one for each hypertext link in the the document.

`links.length`

  A read-only integer specifying the number of elements in the `links[]` array.

`location`

  A synonym for the `URL` property. Not the same as the Location object `window``.location`.

`plugins[]`

  A synonym for the `embeds[]` array. Navigator 3.0 and higher.

`plugins.length`

  The number of elements in the `plugins[]` or `embeds[]` array. Navigator 3.0 and higher.

`referrer`

  A read-only string that specifies the URL of the document that contained the link that referred to the current document.

`title`

  A read-only string that specifies the `<TITLE>` of the document.

`URL`

  A read-only string that specifies the URL of the document.

`vlinkColor`

  A string that specifies the color of visited links. May be set in the document `<HEAD>` or through the `VLINK <BODY>` attribute.

## Methods

`clear()`

  A method that erases the contents of the document. This method is deprecated in Navigator 3.0.

`close()`

  A method that closes a document stream opened with the `open()` method.

`open()`

  A method that opens a stream to which document contents may be written.

`write()`

  A method that inserts the specified string or strings into the document currently being parsed, or into a document stream opened with `open()`.

`writeln()`

  A method identical to `write()`, except that it appends a newline character to the output.

## Event Handlers

`onload`

  An event handler invoked when the document is fully loaded, specified by the `onLoad` attribute of `<BODY>`. Although this event handler is specified as an attribute of the `<BODY>` tag, it is a property of the Window object, not the Document object. See "Window.onload()" for details.

`onunload`

  An event handler invoked when the document is unloaded, specified by the `onUnload` attribute of `<BODY>`. Although this event handler is specified as an attribute of the `<BODY>` tag, it is a property of the `Window` object, not the `Document` object. See "Window.onunload()" for details.

## HTML Syntax

The Document object obtains values for a number of its properties from attributes of the HTML `<BODY>` tag. Also, the HTML contents of a document appear within the `<BODY>` and `</BODY>` tags.

    <BODY
      [ BACKGROUND="imageURL" ]    a background image for the document
      [ BGCOLOR="color" ]  a background color for the document
      [ TEXT="color" ]     the foreground color the document's text
      [ LINK="color" ]     the color for unvisited links
      [ ALINK="color" ]    the color for activated links
      [ VLINK="color" ]    the color for visited links
      [ onLoad="handler" ]     JavaScript to run when the document is loaded
      [ onUnload="handler" ]   JavaScript to run when the document is unloaded
    >
        HTML document contents go here.
    </BODY>

## Description

The Document object represents the HTML document displayed in a browser window or frame. The properties of this object provide details about many aspects of the document, from the colors of the text, background, and anchors, to the date on which the document was last modified. The Document object also contains a number of arrays that describe the contents of the document. The `links[]` array contains one Link object for each hypertext link in the document. Similarly, the `applets[]` array contains one object for each Java applet embedded in the document, and the `forms[]` array contains one Form object for each HTML form that appears in the document.

The `write()` method of the Document object is especially notable. When invoked in scripts that are run while the document is loading, you can call `document.write()` to insert dynamically generated HTML text into the document.

See Chapter 14, Documents and Their Contents, for an overview of the Document object, and of many of the other JavaScript objects that it refers to. See the individual reference pages for full documentation for each of the properties and methods of this object.

## See Also

"Form", "Window", Chapter 14, Documents and Their Contents

document |  | Document.alinkColor