Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
63
rated 0 times [  69] [ 6]  / answers: 1 / hits: 79300  / 13 Years ago, wed, july 20, 2011, 12:00:00

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; nt&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;n&lt;html&gt;n&lt;head&gt;nt&lt;title&gt;Greetings!&lt;/title&gt;n&lt;/head&gt;n&lt;body&gt;nt&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>

More From » html

 Answers
39

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).


[#91106] Monday, July 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileent

Total Points: 556
Total Questions: 107
Total Answers: 101

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
;