Remove all content using pure JS

前端 未结 9 2212
南方客
南方客 2020-12-03 09:56

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          


        
相关标签:
9条回答
  • 2020-12-03 10:56

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

    0 讨论(0)
  • 2020-12-03 10:57
    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

    0 讨论(0)
  • 2020-12-03 10:58

    After the page has already fully loaded:

    document.write('');
    document.close();
    
    0 讨论(0)
提交回复
热议问题