Since we are several 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 attached to "document.body". Some reading ( https://html.spec.whatwg.org/multipage/dom.html#the-body-element-2 ) made me realize that the document attribute "body" can be either "null" or contain an object of type "body" or (not recommended) "frameset".
The following shows if it doesn’t work , i.e. creates a blank page because the purpose of "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 could argue whether adding a paragraph by evaluating innerHTML is a better way, but that's a different story.
JosefScript
source share