Jump to content

JavaScript fails XHTML validator


criley

Recommended Posts

Hi all,First time poster, long time human.I have an inline line of JavaScript that fails the W3C validator for XHTML 1.0 Strict. (http://validator.w3.org/detailed.html)Here's the code:

	for(i=selectbox.options.length-1;i>=0;i--)	{		...code;...	}

At the end of the for statement, the validator seems to see the two dashes and right parenthesis as a mal-formed HTML comment delimiter. I.e. "--)". Anyone know how to prevent this error, or why it even shows up in a JavaSript statement? Frankly, I didn't think the validator would even look at the JavaScript because of the SCRIPT delimiter. I thought it would see this and just ignore it since that's not where HTML would ever be placed (unless embedded into the script with print statements).My JavaScript delimiter:<script type="text/javascript"><!--- ...code...//---></script>My DTD statement:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-ca" lang="en-ca">Any ideas?Thanx.

Link to comment
Share on other sites

Try telling the parser that there is character data to follow like so.<script><![CDATA[put all javascript here ...function matchwo()]]></script>
Thanx Scott. Unfortunately the validator just passes over the declaration and continues to show the same error.C
Link to comment
Share on other sites

The best thing I can suggest is to simply call your JS code from another file like you should anyway.

Link to comment
Share on other sites

I got this reply from a member of the W3C working group about some "unfortunate rules about XML". Interesting:

The validator's error is expected. In XHTML, the "script" element uses adifferent content model than in HTML [1]. The result is that the XMLcomment surrounding your JavaScript is actually a real XML comment, whichhas unfortunate rules about "--" within the comment [2].You can make the error go away by replacing "i--" with "i -= 1" or byremoving the XML comment tokens from your "script". Note that the XML comment would cause your JavaScript to be ignored by an XHTML browser, but this generally doesn't happen in practice because most browsers treat XHTML as HTML.For a more complete discussion, see <http://lachy.id.au/log/2005/05/script-comments>.[1] http://www.w3.org/TR/xhtml1/#h-4.8[2] http://www.w3.org/TR/REC-xml/#sec-comments
Chris
The best thing I can suggest is to simply call your JS code from another file like you should anyway.
Yup, but I was curious.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...