Forum Moderators: open

Message Too Old, No Replies

CDATA and application/xhtml+xml - It works! But why?

Comparisons provided, just need an expert! ;-)

         

JAB Creations

2:15 am on Dec 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a, "It was working, then after I changed something it wasn't, and then again after I changed something it worked again, and I don't know why it's working other then a small segment of code I added so please help me understand it" post.

After changing my document to be served as application/xhtml+xml this script stopped working. Well I replaced the <!-- and --> with //<![CDATA[ and //]]> and it now works on the page when it is served either as text/html or application/xhtml+xml (which is great). But I do not understand ~WHY~ it works which matters more then the fact that it indeed now does work.

The original code is below...just replace as I mentioned above to get it working...but I am curious what CDATA is and why it's letting the code work. Before I had an error in Firefox's JS console saying change was not defined.

<style type="text/css">div.test {
background: #f00;
}
div.testshow {
background: #0f0;
}
</style>

<script type="text/javascript">
<!--
function change(id, newClass)
{
identity=document.getElementById(id);
identity.className=newClass;
}
-->
</script>

<a href="#" onclick="change('test', 'testshow');">Show Test</a> - <a href="#" onclick="change('test', 'test');">Hide Test</a>

<div class="test" id="test">
<span>Tag you're it!</span>
<br />
<a href="#" onclick="change('test', 'test');">Hide Test</a>
</div>

John

Bernard Marx

2:37 am on Dec 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi John, I think I warned you about those <!-- --> comments a while back.

The CDATA tags mark out sections that contain "Raw character data that will not be interpreted as XML markup." I think, perhaps, it may be the semi-colons that were causing grief.

Old style HTML commenting of script & style sections can also trip up if the client decides to internally ignore all such comment elements and not incorporate them into the doc's DOM (as they are allowed to do).

JAB Creations

3:32 am on Dec 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now that I think of it Firefox was complaining about the & inside of the JavaScript...

So the golden rule for application/xhtml+xml is to use CDATA in place of <-- I presume from your post.

I honestly do not recall when you mentioned this but I'm sure you have.

Thanks!

John