90

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>
0

6 Answers 6

116

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What do you do when you want to include ']]>' as part of a string?
@dreamlax: You'll have to break it up into pieces. People have the same problem with </script> which will usually gets broken up into </scr and ipt>.
Actually there may be a point in using html comments around js code still today. Googlebot will crawl any string found in js code that "looks like an url" (according to I-don't-know what criteria) as if it were a link. I know it's totally demential but it does. In many cases you may have strings that look like urls but are not valid urls, so you don't want Google's crawler to annoy your server with nonsense requests. Some guy who seems to know what he says ensures the googlebot won't crawl look-like-url js strings if the js code is enclosed in html comments. haven't tried though
@dreamlax ]]>]]<![CDATA[>
If the goal is to convience HTML/XML validators (nowadays many understand the <script> stuff correctly), then I do not see a significant advantage of the CDATA block above the traditional comment. The only difference is whether the <script> officially has content or not - and regarding search engines, we may want it to have no content...
26

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

2 Comments

No, Internet explorer 2. Netscape had Javascript support from 2.0
@kangax: Worse than that — Netscape Navigator 2 if I remember correctly.
17

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.

3 Comments

Sometimes having all your javascript in .js files instead of in the HTML, makes it harder to update because .js files may be cached.
I link my javascript files from a folder which has in its name the current version of my web application.
Another common tactic of many websites and frameworks, such as wordpress, is to add a query string to the js file URI, in order to force the file to reload when the query string changes. eg: <script type="text/javascript" src="http://example.com/path/to/file.js?v1.0"></script>
8

Not the way you are doing it, no.

The <!-- is treated the same as // in javascript, so your code should instead look like this:

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

..the difference with yours being that a rare obscure browser that may not understand the script tag will print the opening // to the screen, which kind of defeats the purpose of putting the comment tag there in the first place.

Here's more info on it here if you are curious: http://www.javascripter.net/faq/comments.htm

However in the end even super obscure browsers that don't support javascript by default (like HTMLLayout browse or Netsurf) know it is best not to render the text between script tags, so no, it is no longer relevant by any means. However all browsers you could possibly care about understand the <!-- syntax, so there is no real need to madly worry about removing it from what you already have, because it is valid js, just remember to not add it next time.

1 Comment

That's not completely true: HTML comments are valid only within HTML pages (and incidentally, script or even style tags). However, you cannot use HTML comments within js or css files, for example (it would cause a syntax error).
6

Even in modern browsers, it can be useful. I actually ran into this problem today, precisely because I wanted to avoid having javascript embedded in my html.

I have an html page that is served up on http://host/variable_app_name/pagename, where variable_app_name can have many values (y'know, variable). If it wants to access static files it has to use a url like http://host/static/variable_app_name/filename, so I cannot specify the static file location without first looking at browser's location to find the value of variable_app_name.

To link to the main javascript file I do the following:

<script type="text/javascript" >
   var variable_app_name = window.location.pathname.split('/')[1];
   document.write('<script type="text/javascript" src="/static/'+variable_app_name+'/pagename.js"></script>\n');
</script>

The above code will explode even in the latest version of Chrome, because the script tag will be terminated in the middle of a javascript string and the remainder of the string will be interpreted as html, like so:

<script type="text/javascript" >
   var variable_app_name = window.location.pathname.split('/')[1];
   document.write('<script type="text/javascript" src="/static/'+variable_app_name+'/pagename.js">
</script>
\n');
</script>

There are many ways to fix this, but I like to use an html comment.

With html comment:

<script type="text/javascript" >
<!--
   var variable_app_name = window.location.pathname.split('/')[1];
   document.write('<script type="text/javascript" src="/static/'+variable_app_name+'/pagename.js"></script>\n');
-->
</script>

Breaking up the javascript string:

<script type="text/javascript" >
   var variable_app_name = window.location.pathname.split('/')[1];
   document.write('<script type="text/javascript" src="/static/'+variable_app_name+'/pagename.js"></scr'+'ipt>\n');
</script>

Create and append the script tag rather than using document.write:

<script type="text/javascript" >
   var variable_app_name = window.location.pathname.split('/')[1];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = '/static/'+variable_app_name+'/pagename.js';
   document.head.appendChild(script);
</script>

I like to use the html comment because it's a concise change and it won't need replicating or thinking about for each linked file.

Comments

-1

if your looking to comment out js include lines or a complete js block of code, just rename the tag name like below:

Example before:

<script src="js/notification.js"></script>

Example After:

<script__ src="js/notification.js"></script__>

1 Comment

What the hell is that??? If you want to comment out a tag, use HTML comments: <!--script src="js/notification.js"></script--> not invalid HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.