Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

`Document.body` and `Document.title`: page content and title

Use `document.body` to access the page’s content element and `document.title` to read or change the document’s title text.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

doc.body              // Read: HTMLBodyElement, HTMLFrameSetElement, or null
 doc.body = bodyElement // Write: supply an HTML body or frameset element
 doc.title             // Read: string
 doc.title = titleText // Write: supply title text

Document.body/title is shorthand for two properties of the Document interface, not syntax you can use in code. In a page script, document is the current document object. Both properties are web-platform APIs, not features of ECMAScript 2024. A runtime without a DOM need not provide document.

document.body returns the first direct child of the html element that is a body or a legacy frameset. If there is no such child, it returns null. A script running during parsing may be too early to find the body: check that it exists before using it, or run the code after parsing, for example in a DOMContentLoaded handler registered before the event fires.

To add content without replacing the whole element, use document.body.append(node). Assigning a different body or frameset element replaces the existing one and its subtree, or inserts the new element under the document root if there is no existing one. Assigning null does not remove the body; it throws HierarchyRequestError. That exception is also thrown if there is no root element to insert the new element under.

document.title returns a string, not an element or an h1 heading. When read, it trims ASCII whitespace at both ends and collapses internal runs to single spaces. In HTML, a missing title element means an empty string. Assignment updates the title text without parsing it as HTML and typically changes the tab label for an open page. If title is missing but head exists, the assignment creates a title in head; if both are missing, it does nothing. A document rooted at an SVG svg element uses a separate SVG-namespace title directly under that root.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Document.body and Document.title example</title>
</head>
<body>
  <script>
    const doc = document.implementation.createHTMLDocument();
    doc.title = "  Study   notes  ";

    const paragraph = doc.createElement("p");
    paragraph.textContent = "Review the DOM.";
    doc.body.append(paragraph);

    console.log(doc.title);
    console.log(doc.body.textContent);
  </script>
</body>
</html>

Result

Study notes
Review the DOM.

Save the example as an `.html` file, open it in a browser, and check the developer-tools console. The script creates a separate document, so it leaves the open page’s content and tab title unchanged.

Related lessons

  • JavaScript · Lesson 36 · Intermediate · Version: 3

Sources