37

I'm looking for a way to remove the entire content of a web page using pure Javascript -- no libraries.

I tried:

document.documentElement.innerHTML = "whatever";

but that doesn't work: it replaces the inside of the <html/> element. I'm looking at replacing the entire document, including if possible the doctype and <?xml declaration.

Félix Saparelli
  • 8,424
  • 6
  • 52
  • 67

11 Answers11

50

I think a browser rightfully assumes a page with content-type text/html will always be a web page - so whilst you may do something like...

document.body.innerHTML = '';

It will still have some HTML hanging around.

You could try...

document.documentElement.innerHTML = '';

...which left me with <html></html>.

Yi Jiang did suggest something clever.

window.location = 'about:blank';

This will take you to a blank page - an internal mechanism provided by most browsers I believe.

I think however the best solution is to use document.open() which will clear the screen.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • The first updated answer was half of what I was looking for. But I guess changing the `doctype` is asking a bit too much... – Félix Saparelli Nov 22 '10 at 01:05
  • 1
    @passcod Once you change the doctype too, you are changing how the browser will render your HTML/CSS. This will *probably* entail a page reload. I don't think you can change a doctype on the fly. – alex Nov 22 '10 at 01:08
  • 1
    @alex changing **window.location** to **'about:blank'** has a problem with IE specialy IE10 because some addons when detect **about:blank** page fill it with their contents – iman Oct 20 '13 at 08:14
  • My solution seems more appropriate, and avoids the `about:blank` hack. – Mathias Lykkegaard Lorenzen Oct 24 '14 at 22:30
  • @MathiasLykkegaardLorenzen I forgot to add that to my answer, as I suggested [here](http://stackoverflow.com/questions/4241397/remove-all-content-using-pure-js/4241420?noredirect=1#comment28883886_4241519). – alex Oct 26 '14 at 22:02
4
var i = document.childNodes.length - 1;

while (i >= 0) {
  console.log(document.childNodes[i]);
  document.removeChild(document.childNodes[i--]);
}

Removes everything (doctype also) on FF 3.6, Chrome 3.195, and Safari 4.0. IE8 breaks since the child wants to remove its parent.


Revisiting a while later, could also be done like this:

while (document.firstChild) {
  document.removeChild(document.firstChild);
}
Ben
  • 54,723
  • 49
  • 178
  • 224
3

According to Dotoro's article on the document.clear method, they (since it's deprecated) recommend calling document.open instead, which clears the page, since it starts a new stream.

This way, you avoid the nasty about:blank hack.

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
2

One can remove both the <html> element (document.documentElement) and the doctype (document.doctype).

document.doctype.remove();
document.documentElement.remove();

Alternatively, a loop can be used to remove all children of the document.

while(document.firstChild) document.firstChild.remove();

document.open() or document.write() work as well.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

After the page has already fully loaded:

document.write('');
document.close();
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
0

I believe this will do it

document.clear()  //deprecated

window.location = "about:blank"  //this clears out everything
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
  • That didn't work for me, but I did try it on jsbin.com which may be doing something else behind the scenes. – alex Nov 22 '10 at 01:04
  • 1
    @alex ..yeah I just realized document.clear() has been deprecated. – John Hartsock Nov 22 '10 at 01:05
  • @John Hartsock changing **window.location** to **'about:blank'** has a problem with IE specialy IE10 because some addons when detect **about:blank** page fill it with their contents – iman Oct 20 '13 at 08:17
  • Five years later, it is not so important that lots of things still don't work in IE, since MS has stopped development of IE in favor of Edge. – David Spector Jan 18 '19 at 20:50
0
document.documentElement.innerHTML='';
document.open();

The Document.open() method opens a document for writing. if you dont use open method, you cant modify Document after set innerhtml to empty string

Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
0

I believe this will still leave the doctype node hanging around, but:

document.documentElement.remove()

or the equivalent

document.getElementsByTagName("html")[0].remove()

Fiddles
  • 2,790
  • 1
  • 32
  • 35
0

Live demo

If youre using jQuery here's your solution

<div id="mydiv">some text</div>
<br><br>
<button id="bn" style="cursor:pointer">Empty div</button>

<script type="text/javascript">
$(document).on('click', '#bn', function() {
  $("#mydiv").empty();
  $("#bn").empty().append("Done!");
});
</script>

If youre using javascript here's your solution

<div id="purejar">some text</div>
<br><br>
<button id="bnjar" onclick="run()" style="cursor:pointer">Empty div</button>

<script type="text/javascript">
var run = function() {
  var purejar = document.getElementById("purejar");
  var bn = document.getElementById("bnjar");
  purejar.innerHTML = '';
  bn.innerHTML = 'Done!';
}
</script>
Dexter
  • 74
  • 8
-1

Im just curious as to why you'd want to do that. Now theres no way that I know of to replace absolutely everything down to the doctype declaration but if you are wanting to go to those lengths why not redirect the user to a specially crafted page that has the template you need with the doctype you need and then fill out the content there?

EDIT: in response to comment, what you could do is strip all content then create an iframe make it fill the entire page, and then you have total control of the content. Be aware that this is a big hack and will probably be very painful - but it would work :)

Darko
  • 38,310
  • 15
  • 80
  • 107
  • To all who asked the reasons... This seemed like an easy way of stripping everything out and starting from scratch in a userscript... I can include whatever JS code I want, and it will run in the page, not in a sandbox. The `doctype` thing is for when I want to use xhtml on a html website. – Félix Saparelli Nov 22 '10 at 01:10
  • 1
    In fact, Steve's solution is great... just have to tweak some stuff to include the `doctype`... – Félix Saparelli Nov 22 '10 at 01:19
  • It's for userscripts. Ofc I don't need IE support. =) – Félix Saparelli Nov 22 '10 at 01:24
  • 1
    @passcod - Changing the doctype is pointless. You wont end up with an XML document - only an HTTP content-type can do that. And if your building your DOM in javascript, you're not going to validate it either. – Alohci Nov 22 '10 at 02:02
-3

REMOVE EVERYTHING BUT --- !DOCTYPE html ---

var l = document.childNodes.length;    
while (l > 1) { var i = document.childNodes[1]; document.removeChild(i); l--; }

TESTED ON FIREFOX WEB INSPECTOR - childNodes[1] IS --- !DOCTYPE html ---

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129