Forum Moderators: open
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
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).