HTML DOM
HTML DOM doctype Property
The doctype property returns the Document Type Declaration associated with the document as a DocumentType node. For a modern page this is the <!DOCTYPE html> declaration at the top of the file.
Definition and Usage
document.doctype returns the Document Type Declaration (the <!DOCTYPE ...> line) as a DocumentType node, or null if the document has no doctype. The node exposes a name property, which is "html" for standard HTML5 pages.
The property is read-only. It is mostly used to confirm that a page is running in standards mode, which happens when a valid doctype is present.
Syntax
document.doctypeProperties of the returned node
| Name | Description |
|---|---|
| name | The name of the document type, usually "html" |
| publicId | The public identifier (empty for HTML5) |
| systemId | The system identifier (empty for HTML5) |
Example
Click the button to read the doctype node and print its name into the output box.
<!DOCTYPE html>
<html>
<body>
<button onclick="showType()">Show doctype</button>
<p id="out"></p>
<script>
function showType() {
const dt = document.doctype;
document.getElementById("out").textContent =
dt ? "Doctype name: " + dt.name : "No doctype found";
}
</script>
</body>
</html>More Examples
You can reconstruct the doctype string from the node. This example builds a readable version of the declaration.
<!DOCTYPE html>
<html>
<body>
<p id="out"></p>
<script>
const dt = document.doctype;
let text = "<!DOCTYPE " + dt.name;
if (dt.publicId) text += ' PUBLIC "' + dt.publicId + '"';
text += ">";
document.getElementById("out").textContent = text;
</script>
</body>
</html>If a page omits the doctype, document.doctype returns null and the browser may switch to quirks mode, which changes how CSS is rendered.
Key Takeaways
- document.doctype returns the <!DOCTYPE> declaration as a node.
- For HTML5 pages the node's name is "html".
- It returns null when no doctype is present.
- A valid doctype keeps the browser in standards mode.
