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

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

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

Edit: There is one situation that I encountered. Some code editors, such as Dreamweaver, get confused by quoting HTML inside a JavaScript string when they are in the "design view" and trying to display it as part of your page.

+65
javascript html
Oct. 15 '08 at 14:02
source share
5 answers

No, absolutely not. Any user agent, search robot, or absolutely anything else these days is smart enough to ignore Javascript if it cannot execute it.

There was only a very short period when it was generally useful, and that was around 1996.

+70
Oct. 15 '08 at 14:05
source share

There is no reason for this, as browsers that require it have by and large disappeared from the Internet.

In fact, this can lead to unforeseen problems with some older browsers trying to interpret the page if it uses XHTML - from developer.mozilla.org :

  • Mozilla 1.1 + / Opera 7

    Do not use CSS or execute JavaScript.

  • Netscape 7.0x / Mozilla 1.0.x

    Do not use CSS, but do JavaScript.

  • Internet Explorer 5.5+

    Cannot display the document.

This site also refers to examples of several problems mentioned above.

+19
Oct. 15 '08 at 14:20
source share

You should use CDATA though ...

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

Because if you have "<", ">", "&" etc. in the code, the code will not check :)

+14
Oct. 16 '08 at 8:12
source share

Heck, no one else needs this, and if you do, you still have problems to take care of. When you really want to support browsers that need it, you need to keep track of a lot more things. Not to mention the lack of css!

However, the big problem is that people do it wrong. Actually your example is incorrect because the line

 --> 

must read

 //--> 

secondly, you enter the attribute "text / JavaScript", which is also wrong. It was "text / javascript" (all lowercase letters), but it is deprecated (see IANA List ), and now it should be "application / javascript" (see another IANA list . However, Douglas Crockford, JS Guru, said you just need to leave it.

Another problem that no one mentioned was the following: HTML doesn't allow "-" in comments, which means you can't use "x--" to reduce x by one.

+11
Mar 24 '09 at 14:48
source share

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

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

Perhaps these two equivalents? Does anyone know if there is an advantage over another?

+1
Oct 30 '08 at 20:50
source share



All Articles