使用纯JS删除所有内容

发布于 2024-10-03 20:12:27 字数 255 浏览 1 评论 0原文

我正在寻找一种使用纯 Javascript 删除网页的全部内容的方法——没有库。

我尝试过:

document.documentElement.innerHTML = "whatever";

但这不起作用:它替换了 元素的内部。我正在考虑替换整个文档,如果可能的话包括 doctype声明。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

一曲琵琶半遮面シ 2024-10-10 20:12:27

我认为浏览器正确地假设内容类型为 text/html 的页面将始终是一个网页 - 因此,尽管您可能会执行类似...的操作,但

document.body.innerHTML = '';

它仍然会包含一些 HTML。

你可以尝试...

document.documentElement.innerHTML = '';

...这给我留下了

蒋毅确实提出了一些聪明的建议。

window.location = 'about:blank';

这将带您进入一个空白页面 - 我相信这是大多数浏览器提供的内部机制。

但我认为最好的解决方案是使用 document.open() 来清除屏幕。

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.

勿挽旧人 2024-10-10 20:12:27
var i = document.childNodes.length - 1;

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

删除 FF 3.6、Chrome 3.195 和 Safari 4.0 上的所有内容(还有文档类型)。 IE8 崩溃了,因为子级想要删除其父级。


过一段时间再看,也可以这样做:

while (document.firstChild) {
  document.removeChild(document.firstChild);
}
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);
}
七度光 2024-10-10 20:12:27

根据 Dotoro 关于 document.clear 方法的文章,他们(因为它已被弃用)建议调用 document.open 相反,它会清除页面,因为它启动了一个新流。

这样,您就可以避免令人讨厌的 about:blank hack。

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.

南笙 2024-10-10 20:12:27

可以删除 元素 (document.documentElement)和文档类型(document.doctype)。

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

或者,可以使用循环来删除文档的所有子级。

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

document.open()document.write() 也能工作。

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.

流云如水 2024-10-10 20:12:27

页面完全加载后:

document.write('');
document.close();

After the page has already fully loaded:

document.write('');
document.close();
风吹雨成花 2024-10-10 20:12:27

我相信这会做到

document.clear()  //deprecated

window.location = "about:blank"  //this clears out everything

I believe this will do it

document.clear()  //deprecated

window.location = "about:blank"  //this clears out everything
浮生未歇 2024-10-10 20:12:27

我相信这仍然会让 doctype 节点悬而未决,但是:

document.documentElement.remove()

或等效的

document.getElementsByTagName("html")[0].remove()< /代码>

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

document.documentElement.remove()

or the equivalent

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

沫雨熙 2024-10-10 20:12:27
document.documentElement.innerHTML='';
document.open();

Document.open() 方法打开一个文档进行写入。
如果不使用open方法,则将innerhtml设置为空字符串后无法修改Document

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

南街女流氓 2024-10-10 20:12:27

现场演示

如果您使用 jQuery,这里是您的解决方案

<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>

如果您使用 javascript,这里是您的解决方案

<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>

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>
栩栩如生 2024-10-10 20:12:27

我只是好奇你为什么要这么做。现在,据我所知,没有办法完全替换文档类型声明的所有内容,但如果您想要达到这些长度,为什么不将用户重定向到一个特制的页面,其中包含您需要的模板和您需要的文档类型,然后在那里填写内容?

编辑:为了回应评论,您可以做的是剥离所有内容,然后创建一个 iframe 使其填充整个页面,然后您就可以完全控制内容。请注意,这是一个很大的黑客攻击,可能会非常痛苦 - 但它会起作用:)

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 :)

來不及說愛妳 2024-10-10 20:12:27

删除所有内容,但 --- !DOCTYPE html ---

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

在 Firefox WEB Inspector 上测试 - childNodes[1] IS --- !DOCTYPE html ---

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 ---

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文