99

The following practice is fairly commonplace in the inline JavaScript I have to work with:

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

I know that the point is to prevent browsers that are incompatible with JavaScript from rendering the source, but is this still a best practice today? The vast majority of browsers used today can interpret JavaScript; even modern mobile devices usually don't have trouble.

As for the 'why not?' question: I recently had to spend several hours debugging an issue where someone had left off the '//' in front of a '-->' at the end of a script tag buried deep in some pages, and this was causing mysterious JavaScript errors.

What do you do? Is this still considered a 'best practice?'

system PAUSE
  • 33,478
  • 18
  • 60
  • 59
AndreiM
  • 4,390
  • 3
  • 33
  • 50
  • 5
    possible duplicate of [Does it still make sense to use HTML comments on blocks of JavaScript?](http://stackoverflow.com/questions/204813/does-it-still-make-sense-to-use-html-comments-on-blocks-of-javascript) – kapa Jul 29 '11 at 06:23

10 Answers10

120

The important thing is that nowadays, whether a particular browser supports JavaScript or not is irrelevant (clearly the great majority do) - it is irrelevant because almost all understand script blocks, which means that they know to ignore the JavaScript even if they can't interpret it.

Matt Kruse gives a slightly more detailed explanation on his JavaScript Toolbox site for why specifically not to use HTML comments within script blocks.

Quoted from that page:


Don't Use HTML Comments In Script Blocks

In the ancient days of javascript (1995), some browsers like Netscape 1.0 didn't have any support or knowledge of the script tag. So when javascript was first released, a technique was needed to hide the code from older browsers so they wouldn't show it as text in the page. The 'hack' was to use HTML comments within the script block to hide the code.

Using HTML Comments In Script Is Bad

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

No browsers in common use today are ignorant of the <script> tag, so hiding of javascript source is no longer necessary. In fact, it can be considered harmful for the following reasons:

  • Within XHTML documents, the source will actually be hidden from all browsers and rendered useless
  • -- is not allowed within HTML comments, so any decrement operations in script are invalid
Frankie
  • 23,189
  • 10
  • 74
  • 112
Noldorin
  • 134,265
  • 53
  • 250
  • 293
  • 1
    +1. I knew there was someone who had a lot of information about this topic from my comp.lang.javascript days, I was perusing news group archives when your answer appeared, quoting Matt. – Grant Wagner Apr 30 '09 at 20:32
  • 1
    In XHTML, you could use a CDATA instead of the HTML comment: – Concrete Gannet Mar 06 '13 at 19:44
  • I should add that this 'hack' produces an error in Internet Explorer 9 (A client complained about a page that wasn't working properly and this was the cause) – lordscales91 Mar 21 '18 at 10:06
24

I've stopped doing it. At some point you just have to let go of your NCSA Mosaic.

chaos
  • 115,791
  • 31
  • 292
  • 308
  • 7
    In other words, there is no provided reason as to **why** the OP should stop doing it. You are suggesting he should stop doing it because you did, which is not a good enough reason to be an answer IMO. – Lawrence Aiello Oct 09 '15 at 17:38
  • 1
    @LawrenceAiello: What about the phrase "best practice" makes you think of "facts" and not "opinions"? – chaos Oct 09 '15 at 18:28
  • 5
    well therein lies the ultimate fault in this entire post. It should have been closed because it is a discussion question. – Lawrence Aiello Oct 09 '15 at 18:35
  • 1
    Perhaps when this was posted *six years ago* this qualified as an answer, however by the site's standards today it does not. – j08691 Oct 09 '15 at 18:45
  • 1
    This answer is being discussed on [Meta SO](http://meta.stackoverflow.com/questions/307771/delete-an-answer-or-close-the-question) – CubeJockey Oct 09 '15 at 19:12
10

As per W3C Recommendation it was mainly useful to hide the script data from USER AGENTS.

Quoted from the W3c page :

Commenting scripts in JavaScript The JavaScript engine allows the string "<!--" to occur at the start of a SCRIPT element, and ignores further characters until the end of the line. JavaScript interprets "//" as starting a comment extending to the end of the current line. This is needed 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>
Webrsk
  • 984
  • 2
  • 14
  • 24
8

No, it is a hangover from a workaround used when the script element was first introduced. No browser fails to understand the script element today (even if it understands it as "Script that should be ignored because scripting is turned off or unsupported").

In XHTML, they are actively harmful.

I wrote something about the history of it a while back.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
3

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

Jake McGraw
  • 52,590
  • 10
  • 46
  • 62
2

I would recommend using a CDATA section, as described in this question.

Community
  • 1
  • 1
Mike Harder
  • 1,535
  • 13
  • 12
1

If you are typing manually, I suggest you always use external js files, that would help so much.

Regarding your concern: most browsers are JavaScript safe today. However sometimes people may write simple parsers to fetch a HTML directly - and I must say, the safe quote is really helpful for those clients. Also some non-JS clients like old Lynx would get benefits from this.

Francis
  • 9,720
  • 2
  • 29
  • 37
1

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.

kennebec
  • 94,076
  • 30
  • 99
  • 125
0

I stopped doing that ages ago. You really don't need it in this day and age.

John Topley
  • 107,187
  • 45
  • 188
  • 235
-1

I don't do it but the other day I went to validate my password protected site at w3c. So I had to use their direct input method. It complained about my javascript, so I put the comments back in everything was fine.

JoshBerke
  • 62,464
  • 23
  • 120
  • 162