The reason that your HTML is replaced is because of an evil JavaScript function: document.write()
.
It is most definitely "bad form." It only works with webpages if you use it on the page load; and if you use it during runtime, it will replace your entire document with the input. And if you're applying it as strict XHTML structure it's not even valid code.
the problem:
document.write
writes to the document stream. Calling document.write
on a closed (or loaded) document automatically calls document.open
which will clear the document.
-- quote from the MDN
document.write()
has two henchmen, document.open()
, and document.close()
. When the HTML document is loading, the document is "open". When the document has finished loading, the document has "closed". Using document.write()
at this point will erase your entire (closed) HTML document and replace it with a new (open) document. This means your webpage has erased itself and started writing a new page - from scratch.
I believe document.write()
causes the browser to have a performance decrease as well (correct me if I am wrong).
an example:
This example writes output to the HTML document after the page has loaded. Watch document.write()
's evil powers clear the entire document when you press the "exterminate" button:
I am an ordinary HTML page. I am innocent, and purely for informational purposes. Please do not <input type="button" onclick="document.write('This HTML page has been succesfully exterminated.')" value="exterminate"/>
me!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…