DEV Community

loading...

JavaScript: Problem with document.write

andybullet profile image AndyBullet ・1 min read

Hi guys! I have a problem.
By this script https://pastebin.com/XJ17NJ5E I would like to open the page linked (in this case https://www.google.com/) 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?

Discussion

pic
Editor guide
Collapse
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: developer.mozilla.org/en-US/docs/W...

"[C]alling document.write on a closed (loaded) document automatically calls document.open, 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.

Collapse
andybullet profile image
AndyBullet Author

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

Collapse
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 stackoverflow.com/ as it's more specialized for code help like this.