As we are some years further than the originally accepted answer, I would like to give a more modern one.
In Firefox 50.0.2 you can do this:
document.body = document.createElement("body");
document.body.innerHTML = "<p>Hello World!</p>";
Here the body is created and directly assigned to "document.body".
Some reading (https://html.spec.whatwg.org/multipage/dom.html#the-body-element-2) made me understand, that the document's attribute "body" can either be "null" or contain an object of type "body" or (not recommended) "frameset".
The following does not work, i.e. produces a blank page, because the assignment to "document.body" is missing:
var body = document.createElement("body");
body.innerHTML = "<p>Hello World!</p>";
Instead of document.body = body;
you can do this: document.documentElement.appendChild(body);
, whereas document.firstChild.appendChild(body);
throws an error ("HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy").
One might argue whether adding a paragraph by assessing innerHTML is the best way, but that's another story.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…