使用纯JS删除所有内容
我正在寻找一种使用纯 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我认为浏览器正确地假设内容类型为
text/html
的页面将始终是一个网页 - 因此,尽管您可能会执行类似...的操作,但它仍然会包含一些 HTML。
你可以尝试...
...这给我留下了
。
蒋毅确实提出了一些聪明的建议。
这将带您进入一个空白页面 - 我相信这是大多数浏览器提供的内部机制。
但我认为最好的解决方案是使用 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...It will still have some HTML hanging around.
You could try...
...which left me with
<html></html>
.Yi Jiang did suggest something clever.
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.删除 FF 3.6、Chrome 3.195 和 Safari 4.0 上的所有内容(还有文档类型)。 IE8 崩溃了,因为子级想要删除其父级。
过一段时间再看,也可以这样做:
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:
根据 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.可以删除
元素 (
document.documentElement
)和文档类型(document.doctype
)。或者,可以使用循环来删除
文档
的所有子级。document.open()
或document.write()
也能工作。One can remove both the
<html>
element (document.documentElement
) and the doctype (document.doctype
).Alternatively, a loop can be used to remove all children of the
document
.document.open()
ordocument.write()
work as well.页面完全加载后:
After the page has already fully loaded:
我相信这会做到
I believe this will do it
我相信这仍然会让 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()
Document.open() 方法打开一个文档进行写入。
如果不使用open方法,则将innerhtml设置为空字符串后无法修改Document
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
现场演示
如果您使用 jQuery,这里是您的解决方案
如果您使用 javascript,这里是您的解决方案
Live demo
If youre using jQuery here's your solution
If youre using javascript here's your solution
我只是好奇你为什么要这么做。现在,据我所知,没有办法完全替换文档类型声明的所有内容,但如果您想要达到这些长度,为什么不将用户重定向到一个特制的页面,其中包含您需要的模板和您需要的文档类型,然后在那里填写内容?
编辑:为了回应评论,您可以做的是剥离所有内容,然后创建一个 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 :)
删除所有内容,但 --- !DOCTYPE html ---
在 Firefox WEB Inspector 上测试 - childNodes[1] IS --- !DOCTYPE html ---
REMOVE EVERYTHING BUT --- !DOCTYPE html ---
TESTED ON FIREFOX WEB INSPECTOR - childNodes[1] IS --- !DOCTYPE html ---