Proper use of cdata with XHTML

I like to keep my personal page XHTML valid.  There's a lot of less than signs and semicolons in some javascript I just posted which validators complain quite loudly about.
I tried to use some CDATA (<![CDATA[ ]]>) tags to get past the validator, but the best I managed was to completely bungle up my page by hiding large chunks of it that shouldn't have been hidden.

As long as I'm taking the right approach, can you suggest how to include the un-escaped html below into a validating XHTML document?  If it's not cdata what's the right approach?

<a href="javascript:if (1<2) { alert('One is less than two!'); }">Check it</a>

LVL 18
arantiusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rdcproCommented:
Probably you can use:

<a href="javascript:if (1 &lt; 2) { alert('One is less than two!'); }">Check it</a>


The issue is that there are 5 characters that are reserved for Markup:  <, >, &, " and '

Most of the time the Apostrophe and Quote isn't a problem, but the other three need to be escaped as an entity.

Regards,
Mike Sharp
0
arantiusAuthor Commented:
Un-escaped means ...  not escaped.  Can't replace the < with &lt; and etc.
0
rdcproCommented:
Why can't you replace < with &lt; ?  It validates for me, and also displays correctly (at least in IE 6 and Opera).  But any conforming browser should be able to expand the entity into a pointy bracket.

Remember, I don't mean "un-escaped"...I mean "escaped".  When an entity is parsed, then turned into the character it represents, it's being "expanded".

XHTML is an XML representation of HTML, and pointy brackets are not allowed except as markup.  You'll have the same problem if you use:

<a href="javascript:if (1 &lt; 2 && true) { alert('One is less than two!'); }">

This functions as desired if you write it:

<a href="javascript:if (1 &lt; 2 &amp;&amp; true) { alert('One is less than two!'); }">Check it</a>

Regards,
Mike Sharp
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

rdcproCommented:
BTW, if you have an XHTML validator that complains about the &lt; then I'd question the validity of the validator.

Regards,
Mike Sharp
0
arantiusAuthor Commented:
The question is two parts.  There's an XML thing called CDATA.  I want to know how to properly use it in an XHTML page.

I have some javascript that for my own reasons should stay exactly as it is and not be escaped expanded whatever you want to call it.  I believe CDATA solves this.  How is it used properly if that is true?
0
rdcproCommented:
You can't use CDATA sections in an attribute.  Attributes are parsable character data, and markup must be escaped in attributes regardless of your reasons for keeping it non-parsable.  Remember, XHTML is XML, and therefore must follow all the well-formedness rules of XML.  

The XHTML way of using CDATA is like:

<script type="text/javascript">
<![CDATA[ alert("hello"); ]]>
</script>

HOWEVER this will not work in many current browsers, because of the CDATA block.  You'll have to use external script files:

<script type="text/javascript" src="myScriptFile.js"></script>

and call the code as a function.  

These are your only choices:

1.  Escape the attribute markup characters.
2.  Use external Javascript file.
3.  Forget about using XHTML.

Regards,
Mike Sharp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
J_MakCommented:
0
arantiusAuthor Commented:
Thanks Mike.  I'm working on a home brew blog site, and one of my posts was about a javascript bookmarklet I wrote.  Took a lot of work to fit it in to the 508 byte limit IE puts on bookmarklets and I can't add 1 more byte (well, maybe 2) of code to it so I have to leave it as it is.  I also don't handle posts very well.
I like to remain purely validating XHTML valid, as a learning experience.  This is the information I was looking for, and J_Mak those are good links as well!
0
rdcproCommented:
Well, here's a thought.  Whenever an entity like &lt; is parsed, it is expanded into the character it represents.  It is not kept as 4 one-byte characters (or more, depending on encoding).  An &lt; becomes a <, and occupies the same space in memory that an un-escaped < would.    Is the 508 byte limit the *parsed* size?  If it is, you may not have to worry about replacing < with &lt; etc. as the expanded size won't change.


Regards,
Mike Sharp
0
arantiusAuthor Commented:
I had problems keeping it within the limit because it would escape spaces as %20, and that did factor in.
I didn't test html entity escaping and how that would effect it... I wager, it would be un escaped before it gets added.  I just thought CDATA would solve my problem, but I was wrong =) Thanks again for the effort.
0
rdcproCommented:
Oh, I see.  Yeah, if there's URL encoding going on, you're gonna have trouble no matter what.  Even though the parsed entity will only be one character, the URL encoding will turn in into three characters again.

One thing, spaces can be URL encoded with a + sign as well as the %20.  You might be able to scare up a few extra bytes that way.  ;^)

Regards,
Mike Sharp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.