CKTIS / JavaScript
Document.body — accessing the document's body element
Document.body is a Web API property accessed as document.body. It returns a body element, a legacy frameset element, or null. Assignment replaces the entire element, not just its contents.
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.body
document.body = newBodyDocument.body is a property of the Document interface, accessed as document.body. It returns the first direct child of the document element that is a body or frameset element in the HTML namespace. If there is no such child, it returns null. The document element itself does not have to be an html element. It does not return text or a copy of the element.
Account for null before using the result, for example when the parser has not created the body element yet. This is a Web API defined by the HTML Standard, not a feature of ECMAScript itself. The repository's Node version does not establish availability of this browser API.
Assigning document.body = newBody replaces the existing element and its subtree; it does not copy the old contents. Assigning the same element does nothing. If the body element is missing, the new element is appended to the root element. A missing root element, or assigning null or a div element, causes a HierarchyRequestError DOMException: body and frameset elements are accepted.
The example operates on a separate document. It demonstrates replacing the element and detaching the previous one without changing the displayed page.
Examples
const doc = document.implementation.createHTMLDocument("");
const original = doc.body;
original.textContent = "Before";
const replacement = doc.createElement("body");
replacement.textContent = "After";
doc.body = replacement;
console.log(doc.body === replacement);
console.log(doc.body.textContent);
console.log(original.parentNode === null);Result
true After 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.