tags:

views:

384

answers:

4

Is it still relevant to use HTML comment tag around JavaScript code?

I mean

<html>
    <body>
        <script type="text/javascript">
            <!--
            document.write("Hello World!");
            //-->
        </script>
    </body>
</html>
+10  A: 

Not really, unless you're targeting 15-year-old browsers.

musicfreak
Was it Netscape 4? :)
kangax
No, Internet explorer 2. Netscape had Javascript support from 2.0
MarkR
@kangax: Worse than that — Netscape Navigator 2 if I remember correctly.
Chuck
+5  A: 

It is better to just avoid JavaScript in the body all together. It makes things easier to update, avoids the needs for comments and forces you to plan for non-JavaScript enabled users as well as users with JavaScript enabled.

scragar
Sometimes having all your javascript in `.js` files instead of in the HTML, makes it *harder* to update because `.js` files may be cached.
Asaph
I link my javascript files from a folder which has in its name the current version of my web application.
herzmeister der welten
A: 

The tags are valid for XML based markup including HTML. Javascript uses a c-style syntax so the commenting should be done with "//" or "/* comment */" for blocks.

whatnick
That was not the question. In the (real) old days, you needed to include javascript in HTML comment because certain browsers did not support it. And the question was if this is still relevant.
Gamecat
The OP has mentioned uses of javascript besides browsers now I am even more confused and the relevance comment is removed. Odd odd.
whatnick
+13  A: 

HTML comments, ie. <!-- -->, are no longer needed. They were intended to allow browsers that didn't understand the <script> tag to degrade gracefully. These browsers, eg. Netscape 1.x are no longer found in the wild. So there is really no point in putting HTML comments in your script tags anymore.

If you want your HTML to validate as XHTML or XML, you probably want to use a commented out CDATA tag.


<script type="text/javascript">
//<![CDATA[
document.write("Hello World!");
//]]>
</script>

The reason for this is so your <, >, &, " and ' that are part of your javascript code won't have to be encoded as &lt;, &gt;, &amp;, &quot; and &apos; respectively.

Asaph
What do you do when you want to include ']]>' as part of a string?
dreamlax