On Jun 23, 5:42 am, Stanimir Stamenkov wrote:
Sun, 22 Jun 2008 18:17:49 -0700 (PDT), /shapper/:
>Why do some pages I have seen have //<![CDATA[ in the beginning
of a script tag before the script itself?
http://www.w3.org/TR/xhtml1/#h-4.8
That is explaining the appearance of '<![CDATA[' (and for that matter
']]>') in script and style elements in XHTML documents, but the
question being asked here is about the sequence '//<!CDATA[' (and
'//]]>' in principle), that is, with two slash characters in front of
the CDATA section markers. Those two slashes make a huge difference to
the significance of '//<![CDATA[' because they are not at all
necessary in XHTML documents. They appear in HTML documents that are
using mark-up that is intended to give the impression that it is
(maybe valid) XHTML (documents exclusively served as text/html and so
only interpreted as HTML by web browsers), and in Appendix C XHTML
documents that are served as both text/html and application/xhtml+xml
depending on content negotiation (or something resembling it to some
degree).
In the case of documents served as exclusively as text/html the '//<!
[CDATA[' character sequence is no more than a javascript comment and
has no meaning or consequences beyond that so it is not necessary and
can be removed. One of the consequences of removing them would be that
such documents would no longer validate as XHTML, but if the intention
is that they never be interpreted as XHTML by any web browsers (but
instead as the tag soup HTML that they actually are) then there is
little sense in validating the mark-up as XHTML. (It makes much more
sense to write valid HTML form the outset).
In the case of Appendix C XHTML documents served as both text/html and
application/xhtml+xml the '//<![CDATA[' (and '//]]>') sequences may be
necessary to achieve consistent handling of the javascript source text
within the script elements, but as soon as that consistency has been
achieved the script is then gong to have to cope with the
inconsistency of interacting with an HTML DOM if the document was
served as text/html and an XHTML DOM if it was served as application/
xhtml+xml. Almost no non-trivial scripts actually can cope with this,
and the extra level of testing and branching on top of what is
necessary for cross browser code, and the added authoring and testing
effort involve in coping with both types of DOM means that scripting
content negotiated documents is extremely rarely even attempts. Even
the W3C, who content negotiate most of their content give up the
attempt as soon as scripting comes into the picture.
So the '//<!CDATA[' may be necessary if scripting content negotiated
Appendix C XHTML, but content negotiated Appendix C XHTML is such a
nightmare to script that mostly nobody even attempts it, and anyone
needing to ask this question is certainly not going to be attempting
it in the foreseeable future.
>Do I need this?
If you're authoring an XHTML document,
The two slash characters are not needed at all in XHTML document.
yes - you would probably need it, but as the above link
suggests an alternative to use external
scripts - then you wouldn't need it.
Another alternative is to convert the mark-up significant characters
into their entity equivalents. This is actually the most reliable
approach to including inline scripts in XHTML documents (that is real
XHTML not HTML in the guise of XHTML). The problem with wrapping
scripts in CDATA markers is that, while it may avoid problems with the
mark-up significant characters such as '<' and '&' that are so common
in script source code, they do not eliminate the issue entirely
because the character sequence ']]>' remains subject to having
significance attached to it by the browser, and ']]>' is a possible
and valid character sequence in javascript source code (i.e. -
if(a[b[c]]5){ ... } -). Thus CDATA sections reduce the issue to the
point its being difficult to observe but do not solve it.