When to use document.implementation.createHTMLDocument?

后端 未结 3 1316
陌清茗
陌清茗 2020-12-15 23:05

What are some use cases and is it deprecated? As I found out at http://groups.google.com/group/envjs/browse_thread/thread/6c22d0f959666009/c389fc11537f2a97 that it\'s \"non-

3条回答
  •  孤城傲影
    2020-12-15 23:14

    Just a cleaner answer besides @Esailija and @Greg answers: This function will create another document outside the tree of current document, and clean all scripts, styles and images from the new document:

    function insertDocument (myHTML) {
        var newHTMLDocument = document.implementation.createHTMLDocument().body;
        newHTMLDocument.innerHTML = myHTML;
        [].forEach.call(newHTMLDocument.querySelectorAll("script, style, img"), function(el) {el.remove(); });
        documentsList.push(newHTMLDocument);
        return $(newHTMLDocument.innerHTML);
    }
    

    This one is fantastic for making ajax requests and scraping the content will be faster :)

提交回复
热议问题