Document.body

Returns the <body> or <frameset> node of the current document, or null if no such element exists.

Syntax

JavaScript
var objRef = document.body; 
document.body = objRef;

Example

JavaScript
// in HTML: <body id="oldBodyElement"></body>
alert(document.body.id); // "oldBodyElement"

var 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 body 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
WHATWG HTML Living Standard
The definition of 'Document.body' in that specification.
Living Standard  
HTML5.1
The definition of 'Document.body' in that specification.
Working Draft  
HTML5
The definition of 'Document.body' in that specification.
Recommendation  
Document Object Model (DOM) Level 2 HTML Specification
The definition of 'Document.body' in that specification.
Recommendation  
Document Object Model (DOM) Level 1 Specification
The definition of 'Document.body' in that specification.
Recommendation Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 26 (probably much earlier) 2 6 9.6 (possibly earlier) 4 (possibly earlier)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) 5 (probably earlier)

See Also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/document/body

API Document HTML DOM NeedsBrowserCompatibility NeedsMarkupWork NeedsMobileBrowserCompatibility Property Reference Référence