Why document.write() destroys any other html you may have had? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why document.write() destroys any other html you may have had?

27th Jul 2019, 5:17 AM
Tharinda Nimnajith
Tharinda Nimnajith - avatar
7 Answers
+ 5
Yashwanth Kumar I think it's because the "root" of the webpage is the "document" and if you command a bunch or line of out put to be written inside it, then, it will be assumed you don't give an Eff about what's in there previously.
28th Jul 2019, 11:35 PM
Fredericks
Fredericks - avatar
+ 5
Thanks!
27th Jul 2019, 9:54 AM
Tharinda Nimnajith
Tharinda Nimnajith - avatar
+ 4
Yashwanth Kumar How should I answer? This method functions like this because they've made it like this. 😂 It's a simple function, if it appended its argument to the end of document, the argument was going to go after </html> tag so it would make problems.
28th Jul 2019, 9:45 AM
ΛM!N
ΛM!N - avatar
+ 3
Because it overwrites your document's text/code. You should use other functions or methods if you don't want your page to become overwritten.
27th Jul 2019, 6:57 AM
ΛM!N
ΛM!N - avatar
+ 3
Why does it overwrites the document text/code?
28th Jul 2019, 9:39 AM
Yashwanth Kumar
+ 2
name tage
28th Jul 2019, 6:14 AM
KMS69
KMS69 - avatar
+ 2
It has to do with the fact that you called the document object and asked it to run the write method with no input and you did not specify a specific tag to write to or encasulate it within a function to limit its scope. This method is “destructive” as it overrides what data was there in the first place.
29th Jul 2019, 3:12 AM
Pedro E. Matos