tags:

views:

101

answers:

3

Why are some JavaScripts encapsulated within these tags:

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

some js here

//--><!]]>
+7  A: 

It makes it valid XHTML.

Jweede
+2  A: 

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.

Kevin
Are there any browsers left that still actually need it?
Russ Cam
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.
Kevin
Williham Totland
@Williham Totland true enough, I have updated the answer to add that in.
Kevin
+10  A: 

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

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

See: http://stackoverflow.com/questions/204813/does-it-still-make-sense-to-use-html-comments-on-blocks-of-javascript

Diodeus