By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,398 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,398 IT Pros & Developers. It's quick & easy.

//<![CDATA[

P: n/a
Hello,

Why do some pages I have seen have //<![CDATA[ in the beginning of a
script tag before the script itself?

Do I need this?

Thanks,
Miguel
Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Do I need this?
If you're authoring an 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.

--
Stanimir
Jun 27 '08 #2

P: n/a
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.
Jun 27 '08 #3

P: n/a
shapper <md*****@gmail.comwrote:
>Hello,

Why do some pages I have seen have //<![CDATA[ in the beginning of a
script tag before the script itself?
>Do I need this?
If you're writing xhtml and you want to be 100% complaint, then yes.

Javascript uses the characters < and & (and &&). In an XML document,
and therefore also in xhtml, those characters have specific meanings:
< starts a tag (like <img...>) and & starts an entity (like &nbsp;).
The xhtml syntax scanner (like at http://validator.w3.org/) only looks
at tags and entities and compares what it finds to the DTD, so it
knows nothing about Javascript. So the <![CDATA[ syntax is used to
tell the scanner that what follows - until it encounters ]]- is
simply character data and does not contain any tags or entities. Since
few browsers nowadays know much of anything about xhtml, these things
are put into Javascript comments, which explains the leading //.

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Jun 27 '08 #4

P: n/a
Tim Slattery <Sl********@bls.govwrote:
>>If you're writing xhtml and you want to be 100% complaint, then yes.
<<Sigh>that should be "compliant", not "complaint"!

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Jun 27 '08 #5

P: n/a
On Jun 23, 5:34*pm, Tim Slattery <Slatter...@bls.govwrote:
Tim Slattery <Slatter...@bls.govwrote:
>If you're writing xhtml and you want to be 100% complaint, then yes.

<<Sigh>that should be "compliant", not "complaint"!

--
Tim Slattery
Slatter...@bls.govhttp://members.cox.net/slatteryt
Hi,

I am a little bit lost. In all my pages I am using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Could someone please tell me what should I use for inline CSS Styles
ans Scripts?

I am using the following:

<style type="text/css">
<!--
.style1 {font-size: 12px;}
-->
</style>

And

<script type="text/javascript">

//<![CDATA[
// Scripts go here
//]]>

</script>

Yes, I want my web pages to be validated by W3C.org validators.

Am I doing this right or should I change something?

Thanks,
Miguel
Jul 8 '08 #6

P: n/a
shapper <md*****@gmail.comwrote:
>I am a little bit lost. In all my pages I am using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
OK, that's XHTML.
>Could someone please tell me what should I use for inline CSS Styles
ans Scripts?
I don't think there's any problem with CSS. AFAIK, the < and &
characters aren't used in CSS.
>And

<script type="text/javascript">

//<![CDATA[
// Scripts go here
//]]>

</script>
That looks right to me. The CDATA directive tells an XHTML parser that
the following should be interpreted as character data, not XMTHL. The
directive and its ender are in Javascript comments, so browsers that
don't know anything about XHTML (most of them, AFAIK) won't see them
and won't be bothered by them.

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Jul 8 '08 #7

P: n/a
shapper wrote:
On Jun 23, 5:34 pm, Tim Slattery <Slatter...@bls.govwrote:
>Tim Slattery <Slatter...@bls.govwrote:
>>>If you're writing xhtml and you want to be 100% complaint, then yes.
<<Sigh>that should be "compliant", not "complaint"!

[...]
I am a little bit lost. In all my pages I am using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
That is unwise.

<http://www.w3.org/TR/xhtml-media-types/#summary>
<http://hixie.ch/advocacy/xhtml>
<http://hsivonen.iki.fi/xhtml-the-point/>
Could someone please tell me what should I use for inline CSS Styles
ans Scripts?
Read the Spec: <http://www.w3.org/TR/xhtml1/#h-4.8>
I am using the following:

<style type="text/css">
<!--
.style1 {font-size: 12px;}
-->
</style>
An XML parser is allowed to parse this as if it were

<style type="text/css">
</style>

Furthermore, any "<", ">" and "&" in the stylesheet need to be escaped with
character references or character entitity references as you are not
declaring them CDATA.
And

<script type="text/javascript">

//<![CDATA[
// Scripts go here
//]]>

</script>
I think (although Hixie's advisory says otherwise) that this would be
syntactically correct in all conceivable cases. A true SGML or an XML/XHTML
parser would regard the `script' element content as CDATA and pass the
script code enclosed in two empty single-line comments to the script engine;
a tag soup parser would not care for the CDATA declaration and pass the
content verbatim to the script engine; an ECMAScript-compliant script engine
would ignore the single-line comments.

However, the CDATA declaration was not needed if you declared and used HTML
in the first place.
Yes, I want my web pages to be validated by W3C.org validators.
Despite the Validator's current default recommendation, XHTML is not
required for that.
Am I doing this right or should I change something?
You should declare and use HTML 4.01 instead.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 8 '08 #8

P: n/a
Tim Slattery wrote:
shapper <md*****@gmail.comwrote:
>I am a little bit lost. In all my pages I am using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

OK, that's XHTML.
>Could someone please tell me what should I use for inline CSS Styles
ans Scripts?

I don't think there's any problem with CSS. AFAIK, the < and &
characters aren't used in CSS.
You are mistaken, and the `>' character is also relevant. For example, this
is Valid CSS:

#foo .bar:before {
content: "< ";
}

#foo .bar:after {
content: " &";
}
>And

<script type="text/javascript">

//<![CDATA[
// Scripts go here
//]]>

</script>

That looks right to me. The CDATA directive tells an XHTML parser that
the following should be interpreted as character data, not XMTHL.
You meant to say: not parsed character data (PCDATA).
The directive and its ender are in Javascript comments, so browsers that
don't know anything about XHTML (most of them, AFAIK) won't see them
and won't be bothered by them.
The *user agent's *tag-soup parser* (there may be others) would "see" them,
but their *script engine* would ignore them when they were passed to it,
instead.
PointedEars
Jul 8 '08 #9

P: n/a
Henry <rc*******@raindrop.co.ukwrote:
>There are a huge number of web pages which are marked-up in an XHTML
style that have been scripted using exclusively HTML DOM scripts and
so must be served with a text/html content type because they would
promptly break in the event of their mark-up ever being interpreted as
XHTML.
Yeah, I'm quite sure that we're in that boat. Some number of years
ago, somebody here though it would be a wonderful idea to code all our
pages in xhtml. But we've never served them that way, always as
text/html. And we don't conform 100% to xhtml standards for a number
of reasons (Struts version, the <maptag weirdness) anyway.

I have no clue how an HTML DOM script would differ from an XHTML DOM
script, and I'm quite sure none of our scripts were written with XHTML
DOM in mind.

At this point, I think we should drop back to HTML 4.something. I see
no advantage at all in trying to use XHTML. But I don't make the
decisions.....

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Jul 9 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.