41

How can I set the innerHTML, or the whole content of an HTML document using javascript?

For example my document would look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-language" content="en"/>
    <title>Webpage Generator</title>
    <script type="text/javascript">
    var newDocument = "&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; \n\t&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;\n&lt;html&gt;\n&lt;head&gt;\n\t&lt;title&gt;Greetings!&lt;/title&gt;\n&lt;/head&gt;\n&lt;body&gt;\n\t&lt;p&gt;Howdy!&lt;/p&gt;\n&lt;/body&gt;\n&lt;/html&gt;";
    document.innerHTML = newDocument;
    </script>
</head>
<body>
</body>
</html>

But the browser would load the following HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Greetings!</title>
</head>
<body>
    <p>Howdy!</p>
</body>
</html>
Web_Designer
  • 72,308
  • 93
  • 206
  • 262

4 Answers4

34

If you don't want to use innerHTML you could use document.write(newDocument);.

If the document hasn't completely loaded, you'll need to put document.open() as well (thanks bažmegakapa).

wchargin
  • 15,589
  • 12
  • 71
  • 110
  • This just appends `newDocument` onto the current document rather than replacing it. – Web_Designer Jul 20 '11 at 07:04
  • 3
    @inquisitive If you're running this command after the document has loaded, it will issue a `document.open()` call as well. So basically everything will be cleared. [jsFiddle Demo](http://jsfiddle.net/URgmZ/1/) – kapa Jul 20 '11 at 07:10
  • 1
    @inquisitive_web_developer Like I said in the comments on my answer, it works the way you want it to if you do it after the document is loaded (e.g. `window.onload`). – Mathias Bynens Jul 20 '11 at 07:10
32

document.innerHTML is new in HTML5 and isn’t supported in all browsers.

document.documentElement refers to the root element of your document, which in this case is the <html> element.

So, you could set document.documentElement.innerHTML. Note that since the DOCTYPE falls outside of that, so there’s no need to include that in the innerHTML.

Example (try running this in your browser’s JS console):

document.documentElement.innerHTML = '<title>Test</title><p>LOLWAT';

Update: document.innerHTML moved from the HTML specification to the DOM Parsing and Serialization spec, and later got removed. The suggested alternative is to use DOMParser:

var doc = (new DOMParser).parseFromString('<!doctype html><title>wat</title>', 'text/html');

Unfortunately, at the time of writing, most browsers don’t support this yet for HTML.

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • This could be helpful, but is there a way to replace not only the `innerHTML` of the `` element, but the whole document including the doctype? – Web_Designer Jul 20 '11 at 07:00
  • 1
    @inquisitive_web_developer You could use something like `window.onload = function() { document.write('lol'); };` which would pave over the entire document. Or you could use a frame/iframe, which seems to be a better solution than what you’re doing now. – Mathias Bynens Jul 20 '11 at 07:01
  • 1
    @Mathias Bynens your method using `window.onload` is quite clever as it sets the content of the document before it has loaded, and thus it replaces the original HTML with the new document rather than appending the new document onto the loaded html page. Thanks +1! – Web_Designer Jul 20 '11 at 07:15
  • @inquisitive_web_developer Actually, it sets it *after* the document has loaded, which is why it overwrites the original HTML. – Mathias Bynens Jul 20 '11 at 09:07
14

Use this code (after current document has been loaded) :

document.open("text/html", "replace");
document.write(htmlCode);  // htmlCode is the variable you called newDocument
document.close();

Live exemple here : http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_open

hope this help ;)

fred727
  • 2,644
  • 1
  • 20
  • 16
  • Yeah but be careful because some browsers (IE and Firefox) will consider `htmlCode` like the new page itself, so if you hit `F5`, the browser will not refresh the original page, but this `htmlCode`. If you want `F5` still working, see my answer. – Matthieu Charbonnier Jul 13 '17 at 08:37
4

Simple:

document.body.parentElement.innerHTML = '<html><body><div>Your code</div></body></html>';

Note that it will not interpret js code embedded in your html code.

Matthieu Charbonnier
  • 2,794
  • 25
  • 33