I am wanting to completely clear the DOM with Javascript.
I have tried a few things, like this:
document.getElementsByTagName("html")[0].innerHTML = "";
document.body.innerHTML = "";
Interestingly, clearing the head like this will generate an error (“invalid target element for this operation”) in IE, but will successfully clear the head in Chrome. However, clearing the body like this works in IE but fails silently in Chrome.
I have also tried
document.childNodes.length = 0;
but this is apparently a read-only property and won’t do anything.
Is there a good cross-browser way to clear the DOM?
This works…
jsFiddle.
Note that the browser will leave the doctype intact.