使用纯JS删除所有内容 [英] Remove all content using pure JS
问题描述
我正在寻找一种方法来使用纯Javascript删除整个网页内容 - 没有库。
我试过了:
document.documentElement.innerHTML =whatever;
但不起作用:它取代了< html />
元素。我正在考虑替换整个文档,包括可能的 doctype
和<?xml
声明。 p>
我认为浏览器正确地假定页面的内容类型为 text / html
将永远是一个网页 - 所以虽然你可以做一些像... ...
document.body.innerHTML ='' ;
它仍然会有一些HTML挂起。
您可以尝试...
document.documentElement.innerHTML ='';
...给我留下了< html>< / html> ;
。
window.location ='about:blank';
这将带您到一个空白页面 - 我相信大多数浏览器提供的内部机制。 p>
我认为最好的解决方案是使用 document.open()
来清除屏幕。
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.
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.
这篇关于使用纯JS删除所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!