DEV Community


Posted on

JavaScript: Problem with document.write

Hi guys! I have a problem.
By this script I would like to open the page linked (in this case and add to the html page a a pair of SCRIPT tags. But after putting it in, it removes the rest of the HTML page. How do I solve it?

Top comments (3)

joekrump profile image
Joe Krump

write() will do what you described as noted at the top of the documentation for the method which is found here:

"[C]alling document.write on a closed (loaded) document automatically calls, which will clear the document."

I'm also curious to know what it is you are trying to do. Maybe append() is what you're looking for.

andybullet profile image

This message appears Uncaught DOMException: Failed to execute 'append' on 'Document': Nodes of type '#text' may not be inserted inside nodes of type '#document'.

jamesthomson profile image
James Thomson

You would probably be better served to explain what you're trying to do; what problem you're trying to solve. Your pastebin code isn't valid JS, you can't open a window and inject a script on that window (for obvious security reasons). Also, try posting at as it's more specialized for code help like this.