Question

Hi I am having a beast of a time removing comments from html code (so innerHTML counting works in IE). I admit I am not the brightest bulb :) I do not have access to (knowledge of) jquery or the source as such to remove them. I am terrible with regex so I havent tried this

I tried the following code

<script>        
function clean(node)
        {
    for(var n = 0; n &lt; node.childNodes.length; n++)
      {
        var child = node.childNodes[n];
        if
        (
          child.nodeType === 8 ||
          (child.nodeType === 3 &amp;&amp; !/S/.test(child.nodeValue))
        )
        {
          node.removeChild(child);
          n--;
        }
        else if(child.nodeType === 1)
        {
          clean(child);
        }
      }
    }
</script>

But this is giving me error in all browsers:

SyntaxError: missing ) after for-loop control
for(var n = 0; n &lt; node.childNodes.length; n++)

Any ideas would be great. There are a bunch of jquery scripts on the page but I have no control over these and they do not have any errors that occur so I do not think this is related.

Thank you in advance

Was it helpful?

Solution

After you remove the comments via Javascript, they will still be visible in View Source since it shows the source from before the Javascript ran.

If you don't want comments to be visible to the user, don't use HTML comments. Use comments in a serverside language inside the server-side blocks. Like:

<%
   //comment: printing variable x
   out.print(x);
%>

OTHER TIPS

I would start by replacing "&lt ;" with "<", and the same goes for your ampersands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top