문제

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

도움이 되었습니까?

해결책

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);
%>

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top