PDA

View Full Version : How to Make This Line of Code Validate?


kraftomatic
04-06-2006, 05:41 PM
Yes yes, I know validation isn't everything. But this one line of JS code is the only thing holding me back. :)

Is there anything I'm missing here, as to why it won't validate correctly?

<script language="javascript" type="text/javascript">document.write("<a href='" + rotatingSponsorURL + "' class='img' target='_blank' title='" + rotatingSponsorText + "'><img src='" + rotatingSponsor + "' class='featured_sponsor' alt='" + rotatingSponsorText + "' /></a>");</script>

From W3:

Error Line 28 column 172: document type does not allow element "a" here.
...' title='" + rotatingSponsorText + "'><img src='" + rotatingSponsor + "' clas
The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).

Any suggestions would be appreciated.

Thanks!

_Aerospace_Eng_
04-06-2006, 05:44 PM
The ideal solution would be to move the script to an external js file but if you prefer to keep in the document then you need to use CDATA comments.
<script type="text/javascript">
//<![CDATA[
document.write("<a href='" + rotatingSponsorURL + "' class='img' target='_blank' title='" + rotatingSponsorText + "'><img src='" + rotatingSponsor + "' class='featured_sponsor' alt='" + rotatingSponsorText + "' /></a>");
//]]>
</script>
The language attribute is also deprecated, don't use it anymore.

kraftomatic
04-06-2006, 06:37 PM
Perfect, thank you. I'll actually make it an include.

oldcrazylegs
04-07-2006, 08:06 PM
simpleset way is to remove the comments and script tags and save it with a .js extension then include it on your page like below.

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

the validator can't see anything inside an included javascript. this is a great way to hide the embed tag to. it no longer validates but most browsers still require you to use it anyway.