The Document.body
property represents the <body>
or <frameset>
node of the current document, or null
if no such element exists.
Syntax
const objRef = document.body document.body = objRef
Example
// Given this HTML: <body id="oldBodyElement"></body> alert(document.body.id); // "oldBodyElement" const aNewBodyElement = document.createElement("body"); aNewBodyElement.id = "newBodyElement"; document.body = aNewBodyElement; alert(document.body.id); // "newBodyElement"
Notes
document.body
is the element that contains the content for the document. In documents with <body>
contents, returns the <body>
element, and in frameset documents, this returns the outermost <frameset>
element.
Though the body
property is settable, setting a new body on a document will effectively remove all the current children of the existing <body>
element.
Specification
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Document.body' in that specification. |
Living Standard | |
HTML 5.1 The definition of 'Document.body' in that specification. |
Recommendation | |
HTML5 The definition of 'Document.body' in that specification. |
Recommendation |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
body | Chrome Full support 1 | Edge Full support 12 | Firefox
Full support
2
| IE Full support 6 | Opera Full support 9.6 | Safari Full support 4 | WebView Android Full support ≤37 | Chrome Android Full support 18 | Firefox Android
Full support
4
| Opera Android Full support 10.1 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 |
Legend
- Full support
- Full support
- See implementation notes.
- See implementation notes.