When to use document.implementation.createHTMLDocument?

后端 未结 3 1304
陌清茗
陌清茗 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:32

    I always use this because it doesn't make requests to images, execute scripts or affect styling:

    function cleanHTML( html ) {
        var root = document.implementation.createHTMLDocument().body;
    
        root.innerHTML = html;
    
        //Manipulate the DOM here
        $(root).find("script, style, img").remove(); //jQuery is not relevant, I just didn't want to write exhausting boilerplate code just to make a point
    
        return root.innerHTML;
    }
    
    
    cleanHTML( '
    hello
    ' ); //returns "
    hello
    " with the page unaffected

提交回复
热议问题