473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

//<![CDATA[

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
9 3244
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
4
by: matatu | last post by:
Hi to all, I have a xml file, a substring like: &lt;a href=&quot;#&quot;&gt;text&lt;/a&gt; which after an xslt trasform is rendered as (using xsl:output method html): &lt;a...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
34
by: Mark Moore | last post by:
It looks like there's a pretty serious CSS bug in IE6 (v6.0.2800.1106). The HTML below is validated STRICT HTML 4.01 and renders as I would expect in Opera, FrontPage, and Netscape. For some...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
3
by: shaun roe | last post by:
a follow up with new problems from my previous post: I have xml encoded in a string with elements like &lt;myElement/&gt; e.g <codeFragment> &lt;myElement&gt;some text&lt;/myElement&gt; </codeFragment> I...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
6
by: tentstitcher | last post by:
Hi all: I have a source xml document with an element of type string. This element contains something like the following: <stringData> &lt;Header&gt; &lt;Body&gt; </stringData> I would like to apply an...
3
by: webmasterATflymagnetic.com | last post by:
Folks, I'm struggling to put the question together, but I have this problem. I have written an HTML form that I can use for data entry. This uses PHP to write a SQL UPDATE command that gets...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.