Are HTML comments inside script tags the best practice?

The following practice is pretty common in embedded JavaScript that I have to work with:

<script type="text/javascript"> <!-- // Code goes here //--> </script> 

I know that the point is to prevent a browser that is incompatible with JavaScript from rendering the source, but is this still the best practice today? The vast majority of browsers in use today can interpret JavaScript; even modern mobile devices usually do not have problems.

As for why not? Question: Recently, I had to spend several hours debugging a problem when someone left "//" before "->" at the end of a script tag deeply immersed in some pages, and this caused mysterious JavaScript errors.

What do you do? Is it still considered "best practice?"

+82
javascript html
Apr 30 '09 at 20:16
source share
10 answers

The important thing is that at the present time, regardless of whether a particular browser supports JavaScript or not, it does not matter (it is obvious that the vast majority do this) - it does not matter, because almost everyone understands script blocks, which means that they know that they ignore JavaScript even if they cannot interpret it.

Matt Cruze gives a slightly more detailed explanation on his JavaScript Toolbox site why not to use HTML comments in script blocks.

Quote from this page:




Do Not Use HTML Comments In Script Blocks

In the ancient days of javascript (1995), some browsers, such as Netscape 1.0, did not have support or knowledge of the script tag. Therefore, when javascript was first released, it took a technique to hide the code from older browsers so that they did not display it as text on the page. "Hack" had to use HTML comments in a script block to hide the code.

Using HTML comments in a script

 // DON'T do this! Code is just representative on how things were done <script language="javascript"> <!-- // code here //--> </script> 

Today there are no widespread browsers that do not know about <script>, so hiding the javascript source is no longer required. In fact, this can be considered harmful for the following reasons:

  • In XHTML documents, the source will actually be hidden from all browsers and will be useless
  • - not allowed in HTML comments, so any reduction operations in the script are invalid
+108
Apr 30 '09 at 20:23
source share

I stopped doing this. At some point, you just need to let go of your NCSA mosaic.

+25
Apr 30 '09 at 20:18
source share

No, this is a hangover from a workaround used when the script element was first introduced. Today, no browser understands the script element (even if it understands it as "Script that should be ignored because scripts are disabled or unsupported").

In XHTML, they are actively harmful.

I wrote something about his story some time ago.

+8
Apr 30 '09 at 20:26
source share

According to the W3C Recommendation, it was mostly useful to hide script data from USER AGENTS.

Quoted from the W3c page:

Commenting scripts in JavaScript The JavaScript mechanism allows the string "<!--" appear at the beginning of a script element and ignores further characters to the end of the line. JavaScript interprets "//" as the beginning of a comment that extends to the end of the current line. This is necessary to hide the string "->" from the JavaScript parser.

  <SCRIPT type="text/javascript"> <!-- to hide script contents from old browsers function square(i) { document.write("The call passed ", i ," to the function.","<BR>") return i * i } document.write("The function returned ",square(5),".") // end hiding contents from old browsers --> </SCRIPT> 
+8
Apr 30 '09 at 20:37
source share

Stopped using it a while ago. Also, according to Douglas Crockford , you can remove the type attribute from script tags, since the only scripting language available in most browsers is JavaScript.

+3
Apr 30 '09 at 20:28
source share

If you type manually, I suggest you always use external js files that would help that way.

Regarding your concern: most browsers today are safe for JavaScript. However, sometimes people can write simple parsers to directly receive HTML code, and I have to say that a safe quote is really useful for these clients. Also, some non-JS clients, such as the old Lynx, will benefit from this.

+1
Apr 30 '09 at 20:21
source share

If you do not include literal text between script tags, that is, if you load scripts from src files, you can forget about the comments.

+1
May 01 '09 at 2:27
source share

I would recommend using the CDATA partition as described in this question .

+1
May 6 '09 at 8:43 a.m.
source share

I stopped doing this many years ago. You really do not need it this day and age.

0
Apr 30 '09 at 20:20
source share

I do not do this, but the other day I went to check my password-protected site on w3c. So I had to use their direct input method. He complained about my javascript, so I added comments in everything that was good.

-one
Apr 30 '09 at 20:21
source share



All Articles