createDocument (W3C DOM Core method)

Spec
Version Depr. Static
DOM2 No No
Browser support (more…)
IE7 FF1.5+ SA3+ OP9+
None Full Full Full
Returns
Document
Throws
INVALID_CHARACTER_ERR, NAMESPACE_ERR, WRONG_DOCUMENT_ERR

Example

var dom = document.implementation.createDocument
    ('http://www.w3.org/1999/xhtml', 'html', null);

dom.documentElement.setAttributeNS
    ('http://www.w3.org/XML/1998/namespace', 'xml:lang', 'en');

The example above creates a Document node in the XHTML namespace, with an <html> element as its root. It then assigns an xml:lang attribute in the XML namespace to that root, giving it the value en.

That creates the bare bones of an XHTML document in English:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"></html>

However this example doesn't have a doctype. To do that we would need to create one using createDocumentType, and then pass the created doctype as the third argument to createDocument (where currently there’s null). See createDocumentType for details.

Arguments

namespace (DOMString) required

The namespaceURI of the document element.

name (DOMString) required

The qualified name 1 of the document element.

doctype (DocumentType) required

The type of document to be created, or null to not specify a document type. If a document type is specified then its ownerDocument property is set to the document being created.

Description

Create an XML Document of the specified document type, including its root element.

Note: Is this actually useful?

The common use for this was to create a document into which remote XML can be loaded, however this application has largely been superceded by the use of XMLHttpRequest.

Return value

The created document

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
None None None Full Full Full Buggy Buggy Full Full Full

Internet Explorer doesn't implement this method (it returns undefined; in Internet Explorer 5.5 in HTML the implementation object itself is undefined).

Safari 1.3 and 2 do not create the root node (the Document node is created, but its documentElement and firstChild are both null).

Footnotes

1 A qualified name is comprised of a local name, plus an optional prefix delimited with a colon, for example svg:rect.

User-contributed notes

There are no comments yet.

Related Products