← 3.2.5 Content modelsTable of contents4 The elements of HTML →
    1. 3.3 APIs in HTML documents
    2. 3.4 Dynamic markup insertion
      1. 3.4.1 Opening the input stream
      2. 3.4.2 Closing the input stream
      3. 3.4.3 document.write()
      4. 3.4.4 document.writeln()
      5. 3.4.5 innerHTML
      6. 3.4.6 outerHTML
      7. 3.4.7 insertAdjacentHTML()

3.3 APIs in HTML documents

For HTML documents, and for HTML elements in HTML documents, certain APIs defined in DOM Core become case-insensitive or case-changing, as sometimes defined in DOM Core, and as summarized below. [DOMCORE]

This does not apply to XML documents or to elements that are not in the HTML namespace despite being in HTML documents.

Element.tagName and Node.nodeName

These attributes return element names converted to ASCII uppercase, regardless of the case with which they were created.

Document.createElement()

The canonical form of HTML markup is all-lowercase; thus, this method will lowercase the argument before creating the requisite element. .

This doesn't apply to Document.createElementNS(). Thus, it is possible, by passing this last method a tag name in the wrong case, to create an element that appears to have the same tag name as that of an element defined in this specification when its tagName attribute is examined, but that doesn't support the corresponding interfaces. The "real" element name (unaffected by case conversions) can be obtained from the localName attribute.

Element.setAttribute()
Element.setAttributeNode()

Attribute names are converted to ASCII lowercase.

This doesn't apply to Element.setAttributeNS() and Element.setAttributeNodeNS().

Element.getAttribute()
Element.getAttributeNode()

Attribute names are converted to ASCII lowercase.

This doesn't apply to Element.getAttributeNS() and Element.getAttributeNodeNS().

Document.getElementsByTagName()
Element.getElementsByTagName()

HTML elements match by lower-casing the argument before comparison, elements from other namespaces are treated as in XML (case-sensitively).

Thus, in an HTML document with nodes in multiple namespaces, these methods will effectively be both case-sensitive and case-insensitive at the same time.

3.4 Dynamic markup insertion

APIs for dynamically inserting markup into the document interact with the parser, and thus their behavior varies depending on whether they are used with HTML documents (and the HTML parser) or XHTML in XML documents (and the XML parser).

3.4.1 Opening the input stream

The open() method comes in several variants with different numbers of arguments.

document = document . open( [ type [, replace ] ] )

Causes the Document to be replaced in-place, as if it was a new Document object, but reusing the previous object, which is then returned.

If the type argument is omitted or has the value "text/html", then the resulting Document has an HTML parser associated with it, which can be given data to parse using document.write(). Otherwise, all content passed to document.write() will be parsed as plain text.

If the replace argument is present and has the value "replace", the existing entries in the session history for the Document object are removed.

The method has no effect if the Document is still being parsed.

Throws an INVALID_STATE_ERR exception if the Document is an XML document.

window = document . open( url, name, features [, replace ] )

Works like the window.open() method.

3.4.2 Closing the input stream

document . close()

Closes the input stream that was opened by the document.open() method.

Throws an INVALID_STATE_ERR exception if the Document is an XML document.

3.4.3 document.write()

document . write(text...)

In general, adds the given string(s) to the Document's input stream.

This method has very idiosyncratic behavior. In some cases, this method can affect the state of the HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document. In other cases, the call can clear the current page first, as if document.open() had been called. In yet more cases, the method is simply ignored, or throws an exception. To make matters worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.

This method throws an INVALID_STATE_ERR exception when invoked on XML documents.

3.4.4 document.writeln()

document . writeln(text...)

Adds the given string(s) to the Document's input stream, followed by a newline character. If necessary, calls the open() method implicitly first.

This method throws an INVALID_STATE_ERR exception when invoked on XML documents.

3.4.5 innerHTML

The innerHTML IDL attribute represents the markup of the node's contents.

document . innerHTML [ = value ]

Returns a fragment of HTML or XML that represents the Document.

Can be set, to replace the Document's contents with the result of parsing the given string.

In the case of XML documents, will throw an INVALID_STATE_ERR if the Document cannot be serialized to XML, and a SYNTAX_ERR if the given string is not well-formed.

element . innerHTML [ = value ]

Returns a fragment of HTML or XML that represents the element's contents.

Can be set, to replace the contents of the element with nodes parsed from the given string.

In the case of XML documents, will throw an INVALID_STATE_ERR if the element cannot be serialized to XML, and a SYNTAX_ERR if the given string is not well-formed.

3.4.6 outerHTML

The outerHTML IDL attribute represents the markup of the element and its contents.

element . outerHTML [ = value ]

Returns a fragment of HTML or XML that represents the element and its contents.

Can be set, to replace the element with nodes parsed from the given string.

In the case of XML documents, will throw an INVALID_STATE_ERR if the element cannot be serialized to XML, and a SYNTAX_ERR if the given string is not well-formed.

Throws a NO_MODIFICATION_ALLOWED_ERR exception if the parent of the element is the Document node.

3.4.7 insertAdjacentHTML()

element . insertAdjacentHTML(position, text)

Parses the given string text as HTML or XML and inserts the resulting nodes into the tree in the position given by the position argument, as follows:

"beforebegin"
Before the element itself.
"afterbegin"
Just inside the element, before its first child.
"beforeend"
Just inside the element, after its last child.
"afterend"
After the element itself.

Throws a SYNTAX_ERR exception if the arguments have invalid values (e.g., in the case of XML documents, if the given string is not well-formed).

Throws a NO_MODIFICATION_ALLOWED_ERR exception if the given position isn't possible (e.g. inserting elements after the root element of a Document).