DOMImplementation: createDocument() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The createDocument() method of the DOMImplementation interface creates and returns an XMLDocument.
Syntax
createDocument(namespaceURI, qualifiedName)
createDocument(namespaceURI, qualifiedName, documentType)
Parameters
namespaceURI-
A string containing the namespace URI of the document to be created, or
nullif the document doesn't belong to one. qualifiedName-
A string containing the qualified name of the document to be created. A
nullvalue is treated the same as the empty string ("").The format of the qualified name is
prefix:localNameorlocalName, where the parts are defined as:prefixOptional-
A "short alias" for the namespace. The prefix is optional, but if it is specified the
namespaceURIparameter must also be specified. If the prefix is set toxmlorxmlns, thenamespaceURImust be set tohttp://www.w3.org/XML/1998/namespaceorhttp://www.w3.org/2000/xmlns/, respectively. If not set, its value isnull. localName:-
The local name of the document.
documentTypeOptional-
The
DocumentTypeof the document to be created. It defaults tonull.
Return value
The newly-created XMLDocument.
Exceptions
NamespaceErrorDOMException-
Thrown if the
namespaceURIvalue is:- not a valid namespace URI
- set to the empty string when
prefixhas a value - not the value
http://www.w3.org/XML/1998/namespaceorhttp://www.w3.org/2000/xmlns/whenprefixis set toxmlorxmlns, respectively.
InvalidCharacterErrorDOMException-
Thrown if either the
prefixorlocalNameis not valid:- The
prefixmust have at least one character, and cannot contain ASCII whitespace,NULL,/, or>(U+0000, U+002F, or U+003E, respectively). - The
localNameis a valid element name if it has a length of at least 1 and:- it starts with an alphabet character and does not contain ASCII whitespace,
NULL,/, or>(U+0000, U+002F, or U+003E, respectively). - it starts with
:(U+003A ),_(U+005F), or any characters in the range U+0080 to U+10FFFF (inclusive), AND the remaining code points only include those same characters along with the ASCII alphanumeric characters,-(U+002D), and.(U+002E),
- it starts with an alphabet character and does not contain ASCII whitespace,
Note: Earlier versions of the specification were more restrictive, requiring that the
qualifiedNamebe a valid XML name. - The
Examples
>Basic usage
const doc = document.implementation.createDocument(
"http://www.w3.org/1999/xhtml",
"html",
null,
);
const body = document.createElementNS("http://www.w3.org/1999/xhtml", "body");
body.setAttribute("id", "abc");
doc.documentElement.appendChild(body);
alert(doc.getElementById("abc")); // [object HTMLBodyElement]
Specifications
| Specification |
|---|
| DOM> # ref-for-dom-domimplementation-createdocument②> |
Browser compatibility
See also
- The
DOMImplementationinterface it belongs to.