Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are javascripts sometimes surrounded by these tags?

Tags:

javascript

Why are some JavaScripts encapsulated within these tags:

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

some js here

//--><!]]>
like image 307
Drew Avatar asked Apr 26 '10 20:04

Drew


People also ask

Where is the script tag used?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

Do I need to close script tag?

That's because SCRIPT TAG is not a VOID ELEMENT. In an HTML Document - VOID ELEMENTS do not need a "closing tag" at all! In xhtml, everything is Generic, therefore they all need termination e.g. a "closing tag"; Including br, a simple line-break, as <br></br> or its shorthand <br /> .


3 Answers

<![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?

like image 79
Diodeus - James MacFarlane Avatar answered Oct 04 '22 10:10

Diodeus - James MacFarlane


It makes it valid XHTML.

like image 39
Jon W Avatar answered Oct 04 '22 10:10

Jon W


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.

like image 21
kemiller2002 Avatar answered Oct 04 '22 11:10

kemiller2002