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 = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" \n\t"http://www.w3.org/TR/html4/loose.dtd">\n<html>\n<head>\n\t<title>Greetings!</title>\n</head>\n<body>\n\t<p>Howdy!</p>\n</body>\n</html>";
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>
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).