Udostępnij za pośrednictwem


Server Side Comments or how to automatically remove HTML/JavaScript comments from websites

One difference between good and bad code are comments. Good programmers tend to comment their code to make it readable for themselves (the next day ;-) ) or for other programmers (*).

There are situations though where one doesn't want comments to show up - like in an ASP.NET Web Application. Imagine the following simple app.

<head runat="server">

    <title>Untitled Page</title>

    <script type="text/javascript">

        // This function does something

        // incredible :-)

        function DoSomethingIncredible() {

            alert('Hallo');

        }

    </script>

</head>

<body onload="DoSomethingIncredible()">

    <form id="form1" runat="server">

    <div>

   

        <asp:Label ID="Label1" runat="server" Text="Label" /><br />

        <!--

            Don't need this currently but

            am not sure if I need it in

            the future... You get my point ;-)

            <asp:Label ID="Label2" runat="server" Text="Label" />

        -->

    </div>

    </form>

</body>

Unfortunately the comments will be send down to the client together with the remaining markup.

<head><title> Untitled Page</title>    <script type="text/javascript">        // This function does something         // incredible :-)        function DoSomethingIncredible() {            alert('Hallo');        }    </script></head><body onload="DoSomethingIncredible()">    <form name="form1" method="post" action="Default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJOTAxMzgwODE5ZGQvR1v+z54Nn929OgLm6cscsg/+iQ==" /></div>     <div>            <span id="Label1">Label</span><br />        <!--            Don't need this currently but             am not sure if I need it in             the future... You get my point ;-)            <span id="Label2">Label</span>        -->    </div>    </form></body>

Fortunately ASP.NET supports a little known feature called “server-side comments” (<%-- --%>) which removes comments on the server. Check out the enhanced code...

<head runat="server">

    <title>Untitled Page</title>

    <script type="text/javascript">

        <%--

        // This function does something

        // incredible :-)

        --%>

        function DoSomethingIncredible() {

            alert('Hallo');

        }

    </script>

</head>

<body onload="DoSomethingIncredible()">

    <form id="form1" runat="server">

    <div>

   

        <asp:Label ID="Label1" runat="server" Text="Label" /><br />

        <%--

            Don't need this currently but

            am not sure if I need it in

            the future... You get my point ;-)

            <asp:Label ID="Label2" runat="server" Text="Label" />

        --%>

    </div>

    </form>

</body>

...together with the new output (behold the missing comments...). Pretty sweet, eh? 

<head><title> Untitled Page</title>    <script type="text/javascript">                function DoSomethingIncredible() {            alert('Hallo');        }    </script></head><body onload="DoSomethingIncredible()">    <form name="form1" method="post" action="Default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODExMDE5NzY5ZGTf9XGSwEVcluDmIxIb9pffNJ3tXg==" /></div>     <div>            <span id="Label1">Label</span><br />            </div>    </form></body>

Enjoy!

   Daniel

* O.K. Fair enough. This statement only holds true for the somewhat good programmers. The really 1337 programmers naturally don't need no 1@m3 comments ;-)

Comments