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.

Comments on document.open() and the DOM tree of the loaded (closed) browser window on which it works

Parent

document.open() and the DOM tree of the loaded (closed) browser window on which it works

+3
−0

If I execute in browser console:

document.write("Hello");

A new DOM document with the text Hello appears in the same browser window.

From MDN documentation:

Note: Because document.write() writes to the document stream, calling document.write() on a closed (loaded) document automatically calls document.open(), which will clear the document.

What is the meaning of "clear" here?
Does it mean "totally deleted" or is the previous DOM tree stored somewhere and can be retrieved?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+4
−0

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:

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:

// 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), 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).

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

In a glimpse I didn't notice `All existing nodes are removed from the document.` ; But won't it b... (3 comments)
In a glimpse I didn't notice `All existing nodes are removed from the document.` ; But won't it b...
deleted user wrote almost 3 years ago · edited almost 3 years ago

In a glimpse with a quick search I didn't notice All existing nodes are removed from the document.;

Won't it be more correct to say that it's not only the "old" DOM document nodes that are removed, rather the entire "old" DOM document itself?

hkotsubo‭ wrote almost 3 years ago

deleted user My understanding is that the document itself is not removed, only its child nodes. But I'll make more tests as soon as I can

hkotsubo‭ wrote almost 3 years ago · edited almost 3 years ago

deleted user I've made a simple test:

var old = document;
document.write('abc');
console.log(old === document); // true

Which means that the document didn't change (only its child nodes). Another test to check if child nodes changed:

// keep reference to original document's body
var old = document.body;
console.log(old === document.body); // true

// after calling write(), child nodes are removed and a new one is added
document.write('abc');
// document contains a new body, different from the original one
console.log(old === document.body); // false