Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A document.open() and the DOM tree of the loaded (closed) browser window on which it works

At the documetation you linked, if you click on "which will clear the document", it'll go to the documentation for document.open, and that page says in the beginning: All existing nodes are remo...

posted 3y ago by hkotsubo‭  ·  edited 3y ago by hkotsubo‭

Answer
#2: Post edited by user avatar hkotsubo‭ · 2021-11-26T12:33:13Z (almost 3 years ago)
  • At the documetation you linked, if you click on "_which will clear the document_", it'll go to the [documentation for `document.open`](https://developer.mozilla.org/en-US/docs/Web/API/Document/open#notes), and that page says in the beginning:
  • > All existing nodes are removed from the document.
  • And once removed, you can't retrieve them. An alternative is to make a copy of the original document before deleting it:
  • ```javascript
  • // copy document
  • var originalDocument = document.cloneNode(true);
  • // this removes all document's nodes
  • document.write('abc');
  • // but you can restore the original document (not sure if everything will work, such as scripts and event handlers)
  • document.documentElement.replaceChildren(originalDocument.documentElement);
  • ```
  • By using [`cloneNode(true)`](https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode), I create a deep copy of the entire document (`true` indicates that I want to clone not only the `document`, but also all its descendants). So I'll have an entire copy of the DOM tree.
  • In the example above I show how to restore the original document (but I'm not sure if all JavaScript and event handlers will be kept and work properly). But you can actually do anything you want with it (such as use `originalDocument.querySelector` to search for a specific node, access `originalDocument.body`, etc).
  • At the documetation you linked, if you click on "_which will clear the document_", it'll go to the [documentation for `document.open`](https://developer.mozilla.org/en-US/docs/Web/API/Document/open#notes), and that page says in the beginning:
  • > All existing nodes are removed from the document.
  • And once removed, you can't retrieve them.
  • Making a test in this page, this prints a `NodeList` with lots of elements:
  • ```javascript
  • console.log(document.body.childNodes);
  • ```
  • But after doing `document.write('abc')`, the `document.body.childNodes` will have only one element, which is the text node containing "abc".
  • All the original document's child nodes were lost and can't be retrieved from the document anymore.
  • ---
  • A way to avoid losing the original DOM tree is to make a copy of the document before deleting it:
  • ```javascript
  • // copy document
  • var originalDocument = document.cloneNode(true);
  • // this removes all document's nodes
  • document.write('abc');
  • // but you can restore the original document (not sure if everything will work, such as scripts and event handlers)
  • document.documentElement.replaceChildren(originalDocument.documentElement);
  • ```
  • By using [`cloneNode(true)`](https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode), I create a deep copy of the entire document (`true` indicates that I want to clone not only the `document`, but also all its descendants). So I'll have an entire copy of the DOM tree.
  • In the example above I show how to restore the original document (but I'm not sure if all JavaScript and event handlers will be kept and work properly). But you can actually do anything you want with it (such as use `originalDocument.querySelector` to search for a specific node, access `originalDocument.body`, etc).
#1: Initial revision by user avatar hkotsubo‭ · 2021-11-26T12:07:06Z (almost 3 years ago)
At the documetation you linked, if you click on "_which will clear the document_", it'll go to the [documentation for `document.open`](https://developer.mozilla.org/en-US/docs/Web/API/Document/open#notes), and that page says in the beginning:

> All existing nodes are removed from the document.

And once removed, you can't retrieve them. An alternative is to make a copy of the original document before deleting it:

```javascript
// copy document
var originalDocument = document.cloneNode(true);

// this removes all document's nodes
document.write('abc');

// but you can restore the original document (not sure if everything will work, such as scripts and event handlers)
document.documentElement.replaceChildren(originalDocument.documentElement);
```

By using [`cloneNode(true)`](https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode), I create a deep copy of the entire document (`true` indicates that I want to clone not only the `document`, but also all its descendants). So I'll have an entire copy of the DOM tree.

In the example above I show how to restore the original document (but I'm not sure if all JavaScript and event handlers will be kept and work properly). But you can actually do anything you want with it (such as use `originalDocument.querySelector` to search for a specific node, access `originalDocument.body`, etc).