How do I clear off all that was written in a document? Is there a DOM JavaScript API that will erase all that's in the document object's buffer; an equivalent of the server-side Response.Clear()
?
I am just practicing JavaScript.
How do I clear off all that was written in a document? Is there a DOM JavaScript API that will erase all that's in the document object's buffer; an equivalent of the server-side Response.Clear()
?
I am just practicing JavaScript.
Share Improve this question asked Jul 30, 2015 at 18:20 Water Cooler v2Water Cooler v2 33.9k63 gold badges183 silver badges365 bronze badges 3- JavaScript DOM API – crush Commented Jul 30, 2015 at 18:23
-
1
document.write('');
would do... – ᔕᖺᘎᕊ Commented Jul 30, 2015 at 18:23 - What do you mean by "buffer"? – Amit Commented Jul 30, 2015 at 18:23
3 Answers
Reset to default 5here's an alternative that doesn't require querying:
document.documentElement.remove();
You can remove the root (html
) element using:
document.querySelector('html').remove();
or the jQuery equivalent if you have a very old browser:
$('html').remove();
What do you mean by “document object's buffer”? The actual visible content is usually contained in the body, so you can empty it:
document.body.innerHTML = null;
There is no buffer like in server-side preprocessor languages, because the DOM document is entirely client-side rendered.
You can even wipe all the document's descendants, clearing the <html>
element:
document.getElementsByTagName('html').item(0).innerHTML = null;
Also, if you don't like innerHTML
, you can also use textContent
:
document.head.textContent = null;
This is because, quoting MDN:
Setting this property on a node removes all of its children and replaces them with a single text node with the given value.