0

I have a variable that makes a random number from 0 - 20. I wanted it to show up on screen so i did document.write(variable). When i run it the whole page is blank except for the variable. How do you make the variable show up in the screen without having your page destroyed or something. Thank You!

jsalonen
  • 26,663
  • 14
  • 82
  • 104
  • This is what you need: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – elclanrs Apr 27 '13 at 22:54

2 Answers2

2

If you use document.write() after your page is loaded, it will overwrite the HTML document. You can only use this command in the output stream.

Brian McCutchon
  • 7,738
  • 3
  • 27
  • 42
2

Don't use document.write.

Select a DOMElement and set it's text or innerHTML to your random generated number.

for example:

<!DOCTYPE HTML>
<title>Example</title>
<div id="random"></div>
<script>
    var n = String(yourRandomGeneratedNumber);
    document.getElementById('random').innerHTML = n;
</script>

PS: the HTML is valid!

fardjad
  • 18,221
  • 5
  • 48
  • 66