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 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:
But after doing
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:
// copy document
var originalDocument = document.cloneNode(true);
// this removes all document's nodes
// but you can restore the original document (not sure if everything will work, such as scripts and event handlers)
cloneNode(true), 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.
originalDocument.querySelector to search for a specific node, access