Why are some JavaScripts encapsulated within these tags:
<!--//--><![CDATA[//><!--
some js here
//--><!]]>
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.
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 /> .
<![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?
It makes it valid XHTML.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With