Returns the <body> or <frameset> node of the current document, or null if no such element exists.
var objRef = document.body; document.body = objRef;
// 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"
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 | 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 | |
| Document Object Model (DOM) Level 2 HTML Specification The definition of 'Document.body' in that specification. | Obsolete | |
| Document Object Model (DOM) Level 1 Specification The definition of 'Document.body' in that specification. | Obsolete | Initial definition. |
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support | 1 | (Yes) | 2 | 6 | 9.6 (possibly earlier) | 4 (possibly earlier) |
Although document.body is a perfectly acceptable way to select the <body> element in HTML, it's not implemented in Firefox if Content-Type is not set to text/html or application/xhtml+xml. A much safer way of selecting the <body> element is to use document.getElementsByTagName("body"), which should return an array with a single item. This is portable across HTML and XHTML where the Content-Type is not set within the HTTP response header.
| Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | 5 (probably earlier) |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/body