Javascript 使用纯 JS 删除所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4241397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove all content using pure JS
提问by Félix Saparelli
I'm looking for a way to remove the entire content of a web page using pure Javascript -- no libraries.
我正在寻找一种使用纯 Javascript 删除网页全部内容的方法——没有库。
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.
但这不起作用:它替换了<html/>
元素的内部。我正在考虑替换整个文档,如果可能的话,包括doctype
和<?xml
声明。
回答by alex
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...
我认为浏览器正确地假设具有内容类型的页面text/html
将始终是网页 - 所以虽然你可能会做类似......
document.body.innerHTML = '';
It will still have some HTML hanging around.
它仍然会有一些 HTML 挂在周围。
You could try...
你可以试试...
document.documentElement.innerHTML = '';
...which left me with <html></html>
.
...这给我留下了<html></html>
.
Yi Jiangdid 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.
但是我认为最好的解决方案是使用document.open()
它来清除屏幕。
回答by Ben
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.
删除 FF 3.6、Chrome 3.195 和 Safari 4.0 上的所有内容(也包括文档类型)。IE8 中断,因为孩子想要删除其父级。
Revisiting a while later, could also be done like this:
过段时间重温,也可以这样:
while (document.firstChild) {
document.removeChild(document.firstChild);
}
回答by Mathias Lykkegaard Lorenzen
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.
根据Dotoro 关于 document.clear 方法的文章,他们(因为它已被弃用)建议document.open
改为调用,这会清除页面,因为它会启动一个新流。
This way, you avoid the nasty about:blank
hack.
这样,您就可以避免讨厌的about:blank
黑客攻击。
回答by PleaseStand
After the page has already fully loaded:
页面完全加载后:
document.write('');
document.close();
回答by John Hartsock
I believe this will do it
我相信这会做到
document.clear() //deprecated
window.location = "about:blank" //this clears out everything
回答by Kursat Turkay
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
Document.open() 方法打开一个用于写入的文档。如果你不使用 open 方法,你不能在将 innerhtml 设置为空字符串后修改 Document
回答by Fiddles
I believe this will still leave the doctype node hanging around, but:
我相信这仍然会留下 doctype 节点,但是:
document.documentElement.remove()
document.documentElement.remove()
or the equivalent
或等价物
document.getElementsByTagName("html")[0].remove()
document.getElementsByTagName("html")[0].remove()
回答by Darko Z
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?
我只是好奇你为什么要这样做。现在我知道没有办法将所有内容完全替换为 doctype 声明,但是如果您想要达到这些长度,为什么不将用户重定向到一个特制的页面,该页面包含您需要的模板和您需要的 doctype 然后在那里填写内容?
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 :)
编辑:为了回应评论,您可以做的是剥离所有内容,然后创建一个 iframe 使其填满整个页面,然后您就可以完全控制内容。请注意,这是一个大黑客,可能会非常痛苦 - 但它会起作用:)
回答by SNAIL007500
REMOVE EVERYTHING BUT --- !DOCTYPE html ---
删除所有内容,但 --- !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 ---
在 Firefox WEB Inspector 上测试 - childNodes[1] 是 --- !DOCTYPE html ---