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?

ValeriiVasin
  • 8,628
  • 11
  • 58
  • 78
  • This might help: http://stackoverflow.com/questions/5653207/remove-html-comments-with-regex-in-javascript – MassivePenguin Sep 18 '12 at 07:54
  • thanks, I've seen it before, and even implement something like that, but I've researching the quickest way. – ValeriiVasin Sep 18 '12 at 07:56
  • 1
    Using pure JS will be faster than using jQuery. – MassivePenguin Sep 18 '12 at 07:58
  • yep, i'm just researching ways. Could we remove just comment node and replace it with it's content. – ValeriiVasin Sep 18 '12 at 08: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 '' tags then your best bet might be the uncomment plugin, as posted by bart s – MassivePenguin Sep 18 '12 at 08:07
  • I've seen it's code, it's not optimal and is creating extra element. Seems like I can optimize it, but for now, I'm trying to understand maybe will be quicker to replace comments nodes with it's content (and possibly we will not have to create nodes from html strings) – ValeriiVasin Sep 18 '12 at 08:10

6 Answers6

3

Probably using jquery uncomment plugin

bart s
  • 5,068
  • 1
  • 34
  • 55
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.
Stphane
  • 3,368
  • 5
  • 32
  • 47
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);
    });
}

Royce Chao
  • 729
  • 6
  • 5
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

chovy
  • 72,281
  • 52
  • 227
  • 295
1

well get the commented DOM then do a replace()

  var dom =  $.trim($('#domcontainer').html()).replace('<!--','').replace('-->','');
Netorica
  • 18,523
  • 17
  • 73
  • 108
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.

basitmate
  • 81
  • 2
  • 9