Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Document — accessing a page's document

Document represents a DOM document. The document object lets you find elements, read the document root, and use page properties concerning title, focus, and visibility. It is a web-platform interface, not part of ECMAScript itself.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

document.getElementById(id)
document.documentElement
document.body
document.title
document.activeElement
document.hidden
document.addEventListener("visibilitychange", handler)

Document represents a DOM document; in a page script, document refers to the current document. It is neither window nor the root element. This is a web-platform interface: JavaScript availability alone, including Node 24, does not establish support.

Document.getElementById(id) returns the first matching element in tree order or null; pass the identifier without # and check the result before using it. Document.documentElement identifies the root element or is null. On a typical HTML page, this is the html element. Document.body provides the body or frameset element, but can be null, for example before the parser creates it. Document.title lets you read or change the document title.

Document.activeElement identifies the element associated with focus; it need not be a text field. When focus is inside an embedded document or shadow tree, it can identify the embedding element or host, respectively, rather than the deepest element. Document.hidden is a boolean describing document hiddenness, not lack of focus. Document.visibilitychange denotes the visibilitychange event, not a method: handle it with document.addEventListener("visibilitychange", handler) and read document.hidden in the handler.

The example creates a separate HTML document, so it does not modify the open page and produces predictable values. It does not test focus or visibility, which depend on the environment. Source coverage is limited: the supplied DOM Standard defines the Document core, but does not define all the listed HTML extensions concerning title, body, focus, and visibility. Minimum browser versions have not been established here.

Examples

const demo = document.implementation.createHTMLDocument("Document demo");
const item = demo.createElement("p");
item.id = "message";
item.textContent = "Hello, DOM";
demo.body.appendChild(item);

console.log(demo.documentElement.localName);
console.log(demo.body.localName);
console.log(demo.title);
console.log(demo.getElementById("message").textContent);
console.log(demo.getElementById("missing") === null);

Result

html
body
Document demo
Hello, DOM
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources