69

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.

ændrük
  • 782
  • 1
  • 8
  • 22
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • 5
    I guess this has a bit to do with the Ken Keyes, Jr. "monkeys / banana / ladder / water spray" experiment. People keep doing it because they just see it being done that way, but never ask why. – Diodeus - James MacFarlane Oct 15 '08 at 17:00
  • 1
    `-->` should be `//-->`. – Andreas Rejbrand Jul 19 '13 at 22:37
  • 1
    @Diodeus-JamesMacFarlane I'll bite. What's the "_Ken Keyes, Jr. "monkeys / banana / ladder / water spray" experiment_"? Best guess: [This thing](https://hughculver.com/warning-use-5-inspirational-stories-theyre-not-true/) about monkeys cleaning sweet potatoes forever b/c they saw their parents cleaning ones that were sandy (apparently not a true story?), aka a version of [cargo culting](https://en.wikipedia.org/wiki/Cargo_cult_programming) (apparently true. Well, certainly true when it comes to devs ). – ruffin Jan 26 '22 at 15:14
  • @ruffin Or [this story](https://www.youtube.com/shorts/sWEbtgaW5BY) about monkeys that learned to avoid climbing a ladder from other monkeys who had been splashed with water whenever they climbed it. The new monkeys keep doing the same behaviour even though they have never seen the water. – Liam Aug 15 '23 at 16:51
  • 1
    @Liam Except that [that sweet potato link](https://hughculver.com/warning-use-5-inspirational-stories-theyre-not-true/) explains the sweet potato and ladder stories are both made up. And the monkeys that inspired the ladder story were _chimps_. No tail, smart, essentially human. Don't @ me. `:^D` But yeah, lots of good cargo cultists in the software development field. I bet if I put a ladder up at work, put coffee at the top, and had a fire hose... `;^D` And how are there emojis for & but no chimp!?! – ruffin Aug 15 '23 at 21:43

5 Answers5

77

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.

MarkR
  • 62,604
  • 14
  • 116
  • 151
  • 6
    LOL - Guilty! But then, I learned to do this in 1996 and no one has ever said different... – Steven A. Lowe Oct 30 '08 at 19:42
  • I think I have read that it is deprecated but I can't find the link. – some Nov 20 '08 at 16:03
  • Browsers are required to ignore tags they didn't recognize. Netscape Navigator 1.0 had a bug where it would not ignore them, but instead render the text inside. It was fixed in NN 2.0 (1995), but people used comments as a workaround for buggy NN 1.0. It hasn't been an issues for decades - we can stop doing it now. – Ian Boyd Jul 27 '22 at 19:55
21

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.

ConroyP
  • 40,958
  • 16
  • 80
  • 86
14

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 :)

Ionuț Staicu
  • 21,360
  • 11
  • 51
  • 58
  • 12
    IF you want to be XHTML compliant, otherwise you don't need this. – Jason Bunting Oct 30 '08 at 19:21
  • 4
    If you really want to use Javascript in well-formed XML documents, put it in an external .js file. For example, Mozilla XUL applications require this. – MarkR Oct 30 '08 at 21:11
  • heh, the reply was for in document scripts. Ofcourse, i recommend to put all css and jss in external files :) – Ionuț Staicu Dec 02 '08 at 18:49
  • 1
    +1 - everyone should strive to be XHTML compliant, even though you have to account for some quirks. There are various XML parsing libraries/tools that will be less confused with your XHTML if it validates. Let's not turn XHTML into the new sloppy HTML 'transitional'. – andreialecu Sep 16 '09 at 11:11
  • Let's say you want to parse the code for various purpose. If is not valid you may have everything screwed up :) – Ionuț Staicu Jan 02 '10 at 21:40
  • 1
    You can also parse HTML; HTML can also be made valid; http://hixie.ch/advocacy/xhtml ;) – Marcel Korpel May 03 '10 at 22:42
  • 2
    A small difference but personally I prefer using `//<![CDATA[` and `//]]>` - just feels a little neater and easier to read. – DisgruntledGoat Jan 09 '11 at 23:53
  • The `charset` attribute specifies the charset of the file linked to using the `src` attribute; hence, it is not required if there is no `src` attribute. (In fact, in HTML5 it is not even *allowed* if there is no `src` attribute.) – Andreas Rejbrand Jul 19 '13 at 22:44
12

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.

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
1

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?

Andrew Hedges
  • 21,688
  • 16
  • 67
  • 79
  • // is a line comment, /* */ is a block comment. You can use either one, to the javascript it is the same (it is commented). But you forgot to set the type="text/javascript" – some Nov 20 '08 at 15:25
  • 1
    type="text/javascript" is ignored by browsers in favor of the MIME type sent by the server. It's omission was intentional. – Andrew Hedges Sep 21 '09 at 03:23
  • 1
    Andrew: I don't understand. This is a script embedded in the HTML document, so where is the `text/javascript` coming from? Also, I don't think you should use the `charset` attribute if there is no `src` attribute, since the `charset` attribute specifies the charset of the file linked to using the `src` attribute. – Andreas Rejbrand Jul 19 '13 at 22:48