tags:

views:

217

answers:

4

just wondering if there was a way to remove an html comment using jquery.

<!-- <div id="main">Some text </div> -->

thanks

+2  A: 

Comments are not really "in" the DOM in the first place, so I'm going to go out on a limb and say "no". I think I'm really just trying to get Cletus or somebody to drop in and blow my mind with a way to do it :-)

Pointy
Not true. See my answer. Comments have a nodeType value of 8.
patrick dw
I knew it!! I knew it!! (I can't delete the answer until it's unaccepted.)
Pointy
Well, I ain't no Cletus, but I figured there must be a way. ;o)
patrick dw
+1  A: 

I'm almost certain comments aren't actually part of the DOM. They're part of the original HTML code, but when browsers convert it to the DOM, they get stripped as they serve no purpose for rendering.

Pickle
That is not correct.
patrick dw
+1  A: 

Not that I know of. But I don't understand what the use of it would be. A comment will only be seen if you view the pagesource, and most (if not all) browsers that have a view source option will by default give you the source before javascript loading.

Jasper De Bruijn
Yes it is possible.
patrick dw
+3  A: 

Try this:

    $('*').contents().each(function() {
        if(this.nodeType == 8) {
            $(this).remove()
        }
    });

EDIT: This removes the elements from the DOM. Browsers often store a copy of the original page source that is accessible through a menu item. This doesn't get updated.

If you want to hide your comments, you could always insert your entire HTML markup (with comments) into the DOM using javascript. The javascript could, of course, be viewed, but it is a step removed from the first place people would look.

patrick dw
Does that work? I was trying the same but couldn't get any results on FF 3.6...
Sinan Y.
@Sinan - Works for me in Firefox 3.5.8 for Mac. I updated the code from my original version to use `*` instead of `body`, as it wasn't getting comments embedded within descendants of `body`.
patrick dw
@Sinan - Well, I guess it will depend on how you are viewing the source. Using Firebug, they are being removed, but the source that is retrieved using the 'Page Source' menu item never gets updated. It is always in its original state.
patrick dw
@Patrick, I checked my FF now, it's also 3.5.8, sorry i thought it was 3.6 and i'm also on a Mac, anyway i think my firebug doesn't give me proper results. As you say page source doesn't update once loaded, and my firebug doesn't show any HTML comments, so i can't know if comment is removed or not.
Sinan Y.
@Sinan - You can make FF show comments by clicking on the small black triangle in the HTML tab, and select 'Show Comments'. Or, if you're using Safari (or perhaps OmniWeb like I do) you can right click anywhere on the page and click "Inspect Element". Safari's developer tools seems to show comments by default.
patrick dw
_approved_ it works, after activating comments on firebug i was able to see the results, however looping through all elements is a heavy operation not recommended.
Sinan Y.
@Sinan - Yeah, this was just a quick proof of concept. Would be better to limit to the specific areas where the unwanted comments reside.
patrick dw