Section 25.56. Document: an HTML or XML document


25.56. Document: an HTML or XML document

DOM Level 1 Core: Node Document

25.56.1. Subinterfaces

HTMLDocument

25.56.2. Properties


readonly Window defaultView

The web browser Window object (the "view" in DOM terminology) in which this document is displayed.


readonly DocumentType doctype

For XML documents with a <!DOCTYPE< declaration, specifies a DocumentType node that represents the document's DTD. For HTML documents and for XML documents with no <!DOCTYPE>, this property is null.


readonly Element documentElement

A reference to the root element of the document. For HTML documents, this property is always the Element object representing the <html> tag. This root element is also available through the childNodes[] array inherited from Node. See also the body property of HTMLDocument.


readonly DOMImplementation implementation

The DOMImplementation object that represents the implementation that created this document.


readonly CSSStyleSheet[] styleSheets

A collection of objects representing all stylesheets embedded in or linked into a document. In HTML documents, this includes stylesheets defined with <link> and <style> tags.

25.56.3. Methods


addEventListener( )

Adds an event-handler function to the set of event handlers for this document. This is a DOM-standard method supported by all modern browsers except IE.


attachEvent( )

Adds an event-handler function to the set of handlers for this document. This is the IE-specific alternative to addEventListener( ).


createAttribute( )

Creates a new Attr node with the specified name.


createAttributeNS( )

Creates a new Attr node with the specified name and namespace.


createCDATASection( )

Creates a new CDATASection node containing the specified text.


createComment( )

Creates a new Comment node containing the specified string.


createDocumentFragment( )

Creates a new, empty DocumentFragment node.


createElement( )

Creates a new Element node with the specified tag name.


createElementNS( )

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


createEvent( )

Creates a new synthetic Event object of the named type.


createExpression( )

Creates a new XPathExpression object that represents a compiled XPath query. For an IE-specific alternative, see Node.selectNodes( ).


createProcessingInstruction( )

Creates a new ProcessingInstruction node with the specified target and data string.


createRange( )

Creates a new Range object. This method is technically part of the DocumentRange interface; it is implemented by the Document object only in implementations that support the Range module.


createTextNode( )

Creates a new Text node to represent the specified text.


detachEvent( )

Removes an event-handler function from this document. This is the IE-specific alternative to the standard removeEventListener( ) method.


dispatchEvent( )

Dispatches a synthetic event to this document.


evaluate( )

Evaluates an XPath query against this document. See Node.selectNodes( ) for an IE-specific alternative.


getElementById( )

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


getElementsByTagName( )

Returns an array (technically a NodeList) of all Element nodes in this document that have the specified tag name. The Element nodes appear in the returned array in the order in which they appear in the document source.


getElementsByTagNameNS( )

Returns an array of all Element nodes that have the specified tag name and namespace.


importNode( )

Makes a copy of a node from some other document that is suitable for insertion into this document.


loadXML( ) [IE only]

Parses a string of XML markup and stores the result in this document object.


removeEventListener( )

Removes an event handler function from the set of handlers for this document. This is a standard DOM method implemented by all modern browsers except IE.

25.56.4. Description

The Document interface is the root node of a document tree. A Document node may have multiple children, but only one of those children may be an Element node: it is the root element of the document. The root element is most easily accessed through the documentElement property. The doctype and implementation properties provide access to the DocumentType object (if any) and the DOMImplementation object for this document.

Most of the methods defined by the Document interface are "factory methods" that create various types of nodes that can be inserted into this document. The notable exceptions are getElementById( ) and getElementsByTagName( ), which are quite useful for finding a specific Element or a set of related Element nodes within the document tree. Other exceptions are event-handler registration methods such as addEventHandler( ). These event-related methods are also defined by the Element interface and are documented in complete detail there.

You most commonly obtain a Document object via the document property of a Window. Document objects are also available through the contentDocument property of Frame and IFrame, and the ownerDocument property of any Node that has been added to a document.

If you are working with XML (including XHTML), you can create new Document objects with the createDocument( ) method of the DOMImplementation:

 document.implementation.createDocument(namespaceURL, rootTagName, null); 

In IE, you would use code like this instead:

 new ActiveXObject("MSXML2.DOMDocument"); 

See Example 21-1 for a cross-platform utility function that creates a new Document object.

It is also possible to load an XML file from the network and parse it into a Document object. See the responseXML property of the XMLHttpRequest object. You can also parse a string of XML markup into a Document object: see DOMParser.parseFromString( ) and the IE-specific Document.loadXML( ). (Example 21-4 is a cross-platform utility function that uses these methods to parse XML markup.)

See HTMLDocument for additional properties and methods that are specific to HTML documents.

25.56.5. See Also

DOMImplementation, DOMParser, HTMLDocument, Window, XMLHttpRequest; Chapter 15




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net