Validating XHTML with Javascript

May 13th, 2008 Becky Peters

One of the challenges we face when creating and maintaining a standards-compliant XHTML site comes when adding a snippet of code, or including a larger piece of code, written in Javascript and authored by someone else. Why can that pose a challenge? If the code is older or the author is not concerned with XHTML coding standards the code may contain something that causes the validator to throw an error, or it may generate XHTML code that is not in compliance with current standards.

Inquiring minds might pipe up and mention at this point that “in the good old’ days the validator didn’t parse Javascript!” So what’s changed?

In the old days anything between <script></script> was considered CDATA (character data) and ignored by the markup validator. In the here and now it’s considered PCDATA (parsed character data) and it gets parsed! So what’s an intrepid standards-compliant coder to do?

Cleaning up snippets of JS inserted into your page code

A common reason that a web page might be found invalid by the markup validator is due to inserting an older snippet of JS related to some type of interactivity into the page. For instance, adding an onClick event handler:

<a HREF="yourlink.html" onClick="something here">Your Link</a>

This is a no brainer, right? No one can trick you… you know that A HREF is going to be a problem. No Capitalization of Elements is allowed in XHTML! So you fix that:

<a href="yourlink.html" onClick = "something here">Your Link</a>
Read the rest of this article »

Posted in (X)HTML Tricks & Tips, CSS Tricks & Tips, Javascript Snippets & Help, Validation Tips | 2 Comments »