PDA

View Full Version : New to Javascript and have a Validation Error



Rachelrr
01-25-2010, 04:38 AM
Hi Folks,

I'm new here, as well as to JavaScript, and am stuck. I'm trying to get something to validate using the W3C file validation, and I'm stuck. This is XHTML 1.0 Strick, and I know that isn't JS, but since the error involved JS I posted it here. If I would be better putting it in HTML please let me know. Everything works fine on the page, but I keep getting one single error. The > in Red is what shows up as the issue. Do ya'll see something glaringly obvious that I did wrong?

Thanks for the help!



document.write("<a href='mailto:" + emLink + "'>");

mrhoo
01-25-2010, 04:59 AM
You cannot include the characters '<' or '&' in plain text in an xhtml strict document.

Either escape any embedded scripts with cdata comments,
or put the scripts in outside .js text files and load them with <script type="text/javascript" src="url"></script> tags.

Dormilich
01-25-2010, 08:06 AM
Either escape any embedded scripts with cdata comments,
PS. CDATA is not a comment, itís a certain Text node type.

mrhoo
01-25-2010, 03:01 PM
If you merely want the xhtml validator to pass the code,
it is still most practical to comment out the cdata declaration.
The validator ignores any text within a cdata node,
but the browsers ignore the cdata tag.

CDATA script nodes in html do not work correctly in IE6 at least,
and so the cdata syntax itself must be commented.

cdata


<script type="text/javascript">
<![CDATA[
function(){}
]]>
</script>

commented cdata


<script type="text/javascript">
/* <![CDATA[ */
function(){}
/* ]]> */
</script>

The best solution is to keep scripts out of the html, and load them in script tags.

Dormilich
01-25-2010, 03:37 PM
CDATA script nodes in html do not work correctly in IE6 at least, and so the cdata syntax itself must be commented.

CDATA is not part of HTML and is thus not treated as such. to work correctly, CDATA (resp. the document containing CDATA nodes) must be processed through an XML parser (that is, "application/xml", "application/xhtml+xml", "text/xml" (though I’m not sure about that), but definitely not "text/html").

escaping CDATA is only required, if you serve XHTML as "text/html" (in which case you could use HTML from the start)

Rachelrr
01-25-2010, 11:28 PM
Thanks for the help guys. I added CDATA above and below and got it to validate correctly.