Team LiB   Previous Section   Next Section
Document Client-side JavaScript 1.0; DOM Level 1

an HTML document Inherits From: Node (in DOM Level 1)

Synopsis

window.document
document

Description

The Document object represents an HTML document and is one of the most important objects in client-side JavaScript. It was introduced in JavaScript 1.0, and a number of methods and properties were added in JavaScript 1.1. Netscape and Internet Explorer each add non-standard methods and properties to the Document object, and the W3C DOM standardizes additional properties and methods.

Common Properties

All implementations of the Document object support the following properties. This list is followed by separate lists of properties defined by the W3C DOM Document object and by the IE 4 and Netscape 4 Document objects.

alinkColor

A string that specifies the color of activated links. Deprecated.

anchors[ ]

An array of Anchor objects, one for each anchor that appears in the document. JS 1.2.

applets[ ]

An array of Applet objects, one for each applet that appears in the document. JS 1.1.

bgColor

A string that specifies the background color of the document. Deprecated.

cookie

A string-valued property with special behavior that allows the cookies associated with this document to be queried and set.

domain

A string that specifies the Internet domain the document is from. Used for security purposes. JS 1.1.

embeds[ ]

An array of objects that represent data embedded in the document with the <embed> tag. A synonym for plugins[ ]. Some plugins and ActiveX controls can be controlled with JavaScript code. The API depends on the specific control. JS 1.2 .

fgColor

A string that specifies the default text color for the document. Deprecated.

forms[ ]

An array of Form objects, one for each HTML form that appears in the document.

images[ ]

An array of Image objects, one for each image that is embedded in the document with the HTML <img> tag. JS 1.1.

lastModified

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

linkColor

A string that specifies the color of unvisited links. Deprecated.

links[ ]

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

location

The URL of the document. Deprecated in favor of the URL property.

plugins[ ]

A synonym for the embeds[ ] array. JS 1.1.

referrer

A read-only string that contains the URL of the document, if any, from which the current document was linked.

title

The text contents of the <title> tag. Read-only prior to DOM Level 1.

URL

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

vlinkColor

A string that specifies the color of visited links. Deprecated.

W3C DOM Properties

In DOM-compliant browsers, the Document object inherits the properties of Node, and defines the following additional properties.

body

A reference to the Element object that represents the <body> tag of this document.

defaultView

The Window in which the document is displayed. Read-only. DOM Level 2.

documentElement

A read-only reference to the <html> tag of the document.

implementation

The DOMImplementation object that represents the implementation that created this document. Read-only.

IE 4 Properties

The following non-standard (and non-portable) properties are defined by Internet Explorer 4 and later versions.

activeElement

A read-only property that refers to the input element that is currently active (i.e., has the input focus).

all[ ]

An array of all Element objects within the document. This array may be indexed numerically to access elements in source order, or it may be indexed by element id or name.

charset

The character set of the document.

children[ ]

An array that contains the HTML elements that are direct children of the document. Note that this is different than the all[ ] array that contains all elements in the document, regardless of their position in the containment hierarchy.

defaultCharset

The default character set of the document.

expando

This property, if set to false, prevents client-side objects from being expanded. That is, it causes a runtime error if a program attempts to set the value of a nonexistent property of a client-side object. Setting expando to false can sometimes help to catch bugs caused by property misspellings, which can otherwise be difficult to detect. This property can be particularly helpful for programmers who are switching to JavaScript after becoming accustomed to case-insensitive languages. Although expando only works in IE, it can be safely (but ineffectively) set in Netscape.

parentWindow

The window that contains the document.

readyState

Specifies the loading status of a document. It has one of the following four string values:

uninitialized

The document has not started loading.

loading

The document is loading.

interactive

The document has loaded sufficiently for the user to interact with it.

complete

The document is completely loaded.

Netscape 4 Properties

The following non-standard (and non-portable) properties are defined by Netscape 4.

height

The height, in pixels, of the document.

layers[ ]

An array of Layer objects that represents the layers contained within a document. This property is only available in Netscape 4; it has been discontinued as of Netscape 6.

width

The width, in pixels, of the document.

Common Methods

All implementations of the Document object support the following methods. This list is followed by separate lists of methods defined by the W3C DOM standard and by the IE 4 and Netscape 4 Document objects.

clear( )

Erases the contents of the document and returns nothing. This method is deprecated in JavaScript 1.1. JS 1.0; deprecated.

close( )

Closes a document stream opened with the open( ) method and returns nothing. JS 1.0.

open( )

Deletes existing document content and opens a stream to which new document contents may be written. Returns nothing. JS 1.0.

write( value, ...)

Inserts the specified string or strings into the document currently being parsed or appends to document opened with open( ). Returns nothing. JS 1.0.

writeln( value, ...)

Identical to write( ), except that it appends a newline character to the output. Returns nothing. JS 1.0

W3C DOM Methods

In DOM-compliant browsers, the Document object inherits the methods of Node, and defines the following additional methods.

createAttribute( name)

Returns a newly-created Attr node with the specified name.

createComment( text)

Creates and returns a new Comment node containing the specified text.

createDocumentFragment( )

Creates and returns an empty DocumentFragment node.

createElement( tagName)

Creates and returns a new Element node with the specified tag name.

createTextNode( text)

Creates and returns a new Text node that contains the specified text.

getElementById( id)

Returns the Element of this document that has the specified value for its id attribute, or null if no such Element exists in the document.

getElementsByName( name)

Returns an array of nodes of all elements in the document that have a specified value for their name attribute. If no such elements are found, returns a zero-length array.

getElementsByTagName( tagname)

Returns an array of all Element nodes in this document that have the specified tag name. The Element nodes appear in the returned array in the same order they appear in the document source.

importNode( importedNode, deep)

Creates and returns a copy of a node from some other document that is suitable for insertion into this document. If the deep argument is true, it recursively copies the children of the node too. DOM Level 2.

Netscape 4 Methods

getSelection( )

Returns the currently selected document text with HTML tags removed.

IE 4 Methods

elementFromPoint(x,y)

Returns the Element located at a specified point.

Event Handlers

In DOM-compliant browsers and IE 4, the Document object supports the same list of generic event handlers that the Element object does. Although the onload and onunload handlers logically belong to the Document object, they are implemented as properties of the Window object.

See Also

Anchor, Applet, Element, Form, Image, Layer, Link, Window

    Team LiB   Previous Section   Next Section