7

Why are some JavaScripts encapsulated within these tags:

<!--//--><![CDATA[//><!--

some js here

//--><!]]>
1
  • you only use them in a html file. All modern browser don't need it anymore, but the w3c validator does if you use certain characters
    – meo
    Apr 26, 2010 at 20:40

3 Answers 3

10

<![CDATA[//> is to prevent XML parsers from choking on the script.

<!-- ... --> is not required unless you're using Netscape 1.0.

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

6

It makes it valid XHTML.

2

They were used for old browsers which didn't understand the <script> tag. That way if a browser didn't properly read the JavaScript, it would just render it as a comment and not show up on the page.

It also allows JavaScript code to be inside valid XHTML pages without having to escape characters which aren't valid in XML.

3
  • 1
    I was trying to think about that. Not that I know of, but I suppose you could still run into someone who uses it....not that it would (most likely) render any modern page today though. Apr 26, 2010 at 20:39
  • Except for any modern browser in XHTML mode. It's an alternative to escaping all <s and &s in your <script> tag. It also applies to RSS feeds and the like. Apr 26, 2010 at 20:42
  • @Williham Totland true enough, I have updated the answer to add that in. Apr 26, 2010 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.