1

Assume you have a structure of commented html, that comes from server (it's commented for improve rendering speed, like FB). After that we should remove our content and allow browser render html. What is the quickest way to do this?

6
  • This might help: stackoverflow.com/questions/5653207/… Commented Sep 18, 2012 at 7:54
  • thanks, I've seen it before, and even implement something like that, but I've researching the quickest way. Commented Sep 18, 2012 at 7:56
  • 1
    Using pure JS will be faster than using jQuery. Commented Sep 18, 2012 at 7:58
  • yep, i'm just researching ways. Could we remove just comment node and replace it with it's content. Commented Sep 18, 2012 at 8:04
  • Right, sorry, I misunderstood - I thought you were trying to remove the comments (and their content) from the DOM. If you're just removing the '<!--' and '-->' tags then your best bet might be the uncomment plugin, as posted by bart s Commented Sep 18, 2012 at 8:07

7 Answers 7

3

Probably using jquery uncomment plugin

Sign up to request clarification or add additional context in comments.

1 Comment

seems like it's not optimized :) but idea with nodeType === 8 is ok
3

you can use the "content()" method to cet all nodes including comments: light and plugin free !!

Then you should filter the collection using "filter()" method

$container
    .contents()
    .filter(function(){ return this.nodeType == 8; })
    .remove(); // replaceWith()  etc.

1 Comment

Note that .contents() doesn't seek deep.
2
function _removeComments(node) {
    if (!node || !node.parentNode) return;

    if (node.nodeType == 8) {
        node.parentNode.removeChild(node);
        return;
    }

    for (var c = node.firstChild; c; ) {
        var n = c.nextSibling;
        _removeComments(c);
        c = n;
    }
}

function removeComments(element) {
    element.each(function() {
        _removeComments(this);
    });
}

Comments

1

well get the commented DOM then do a replace()

  var dom =  $.trim($('#domcontainer').html()).replace('<!--','').replace('-->','');

1 Comment

I'm using .replace(/<!--([\s\S]*?)-->/mig, '$1');
1
$(stringHtml).comments().remove();

...using the jquery-comments plugin:

http://www.bennadel.com/blog/1563-jquery-comments-plug-in-to-access-html-comments-for-dom-templating.htm

2 Comments

Uncaught TypeError: $(...).comments is not a function
0

Try this method. Provide the element node with the commented HTML and this will remove the comment blocks and reveal the HTML.

function uncommentNode(node) {
    var nodestr = node.innerHTML;
    var noderevealHTML =  nodestr.replace(/<!--/ig, '').replace(/-->/ig, ''); // Update expressions here.
    node.innerHTML = noderevealHTML;
}

If your commented HTML has three or more dashes (<-- / <---) in the comment start/end tag, be sure to update the replace() expressions in the function above.

Here's a JSFIDDLE for better explanation.

Comments

0

Finding them with document.querySelectorAll() is impossible. And jQuery.contents() doesn't seek deep.

But this short logic collects all comments – even those nested deep.

let gatherer = (root) => { 

    return gatherer_rec(root).flat(Infinity);
};
let gatherer_rec = (node) => { 
    
    if (!(node instanceof Node)) {
        return [];
    } 

    if (node.nodeType === Node.COMMENT_NODE) {
        return [node];
    }

    return Array.from(node.childNodes).map((item) => gatherer_rec(item));
};

Once you have a specific comment, you can totally use jQuery methods for the replacement.

$(gatherer(document.body)[54]).replaceWith('hi');

Adjust to your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.