Colin Cochrane

Colin Cochrane is a Software Developer based in Victoria, BC specializing in C#, PowerShell, Web Development and DevOps.

A Code Snippet That Speaks For Itself

While working on a client's site today I came across this gem that I thought you might enjoy.  I'll let the snippet speak for itself.

[code:html]

<script type="text/javascript">
document.write('<');
document.write('!--  ');
</script>
&lt;!--   
<noscript> 
(Removed to protect anonymity of client)
</noscript> 
<!--//-->  

[/code]

*Smacks Forehead*

Comments (5) -

  • Sam

    12/4/2007 3:23:55 PM | Reply

    That looks awful. I am not really familiar with this stuff though--did some code generator make that? If so, which one? I guess if the HTML works, it can't be that bad. A lot of pages don't even work.

  • Colin Cochrane

    12/4/2007 3:42:29 PM | Reply

    I'm sad to say that it is man-made.  The site in question is all hand-coded.

  • Gary

    12/18/2007 4:58:52 AM | Reply

    OK - for those of us who are new to HTML and Javascript yet thoroughly enjoy your work here, can you spell out what's so funny about the code?

  • Colin Cochrane

    12/18/2007 5:17:43 AM | Reply

    You've probably seen web documents where the content of a <script> element has been wrapped in an SGML comment tag like so:
    <script type="text/javascript">
    <!--
    function dosomething(){}
    -->
    </script>

    The purpose of the comment tag is to "hide" the script from older browsers that don't support Javascript.  Without the comment tag those browsers will render the contents
    of the <script> element as normal text.  For instance, a page with the following markup...

    *****************************
    <body onload="dosomething()">
    <h1>Products</h1>
    <script>
    function dosomething(){}
    </script>
    <p> This is my product! </p>
    </body>
    *****************************

    ...would end up displaying as...

    ******************************
    Products
    function dosomething(){}

    This is my product!
    ******************************

    With this in mind, the snippet in the original post is amusing because it's using javascript to output a comment tag to hide the script from those older browsers...that don't support Javascript.

  • Gary

    12/18/2007 5:56:29 AM | Reply

    Thanks for the insight. That is very funny. Keep up the great work!

Loading