Does it still make sense to use HTML comments on blocks of JavaScript?

JavascriptHtml

Javascript Problem Overview


In the past people used to wrap HTML comment tags around blocks of JavaScript in order to prevent "older" browsers from displaying the script. Even Lynx is smart enough to ignore JavaScript, so why do some people keep doing this? Are there any valid reasons these days?

<script type="text/javascript">
<!--
//some js code
//-->
</script>

Edit: There is ONE situation I did encounter. Some code editors, such as Dreamweaver, get confused by quoted HTML inside a JavaScript string when in "design view" and try to display it as part of your page.

Javascript Solutions


Solution 1 - Javascript

No, absolutely not. Any user agent, search engine spider, or absolutely anything else these days is smart enough to ignore Javascript if it can't execute it.

There was only a very brief period when this was at all helpful, and it was around 1996.

Solution 2 - Javascript

There isn't a good reason to do this anymore, as the browsers which required this have by and large disappeared from the web.

In fact, doing this can actually cause unintended problems with certain older browsers' attempts to interpret the page if it uses XHTML - from developer.mozilla.org:

>- Mozilla 1.1+/Opera 7

> Do not apply CSS or execute the JavaScript.

>- Netscape 7.0x/Mozilla 1.0.x

> Do not apply CSS but does execute the JavaScript.

>- Internet Explorer 5.5+

> Can not display the document.

That site also links to examples of the several problems mentioned above.

Solution 3 - Javascript

You should use CDATA though...

<script type="text/javascript" charset="utf-8">
/* <![CDATA[ */
	
/* ]]> */
</script>

Because if you have '<', '>', '&', etc in your code, the code won't validate :)

Solution 4 - Javascript

Hell no, nobody needs this anymore and if you do, you have some more problems to care about. When you really want to support browsers that need that, you have to watch out for a lot more things. Not even talking about the lack of css!

However, the bigger problem is, that people do this wrong. Actually your example is wrong, because the line

-->

should read

//-->

secondly, you type attribute says "text/JavaScript" what is wrong too. It has been "text/javascript" (all lower case) but this is obsolete (see the IANA List) and now it should be "application/javascript" (see another IANA List. However, Douglas Crockford, the JS Guru, said you just should leave it out.

Another Problem nobody mentioned already is this: Within HTML comments, "--" is not allowed and that means you can't use "x--" to decrement x by one.

Solution 5 - Javascript

Not having to use CDATA blocks is one of the reasons I prefer to use HTML 4.01 Strict as my docttype, but, Staicu, I thought it used the following syntax:

<script charset="utf-8">
//<![CDATA[

//]]>
</script>

Maybe the two are equivalent? Anyone know if there is an advantage to one over the other?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDiodeus - James MacFarlaneView Question on Stackoverflow
Solution 1 - JavascriptMarkRView Answer on Stackoverflow
Solution 2 - JavascriptConroyPView Answer on Stackoverflow
Solution 3 - JavascriptIonuČ› StaicuView Answer on Stackoverflow
Solution 4 - JavascriptTim BütheView Answer on Stackoverflow
Solution 5 - JavascriptAndrew HedgesView Answer on Stackoverflow