tl;dr
Download the code. Use $.validateHTML5(callback)
, where callback (result, data)
is a function that is called with the result
, a string that can be "valid"
if the page validated, "invalid"
if it did not, "warning"
if it validated but is "in some way questionable", or "not validated"
if some error occurred. data
is the actual JSON
output from the validator.
Discussion
Lots of sites have links like <a href="http://validator.w3.org/check/referer">Valid HTML</a>
, which claims that the site is valid and allows you to click the link and check it, but it would be nice to have some way of actually validating the page that is being shown. Nice for me as the author, at least. I'm not sure anyone else cares.
Luckily, I've been doing everything recently in HTML5, and Validator.nu has a web service API with JSON output that we can use programmatically. We don't want to do this on the server side, since that takes time that our users don't need to waste. Also (I did this when playing around with it) you can end up in an infinite loop, where my server GETs the validator output, which GETs the page from the server, which GETs the validator output, which GETs the page from the server, ad infinitum.
So we want to send the page without validating it and then use AJAX to validate it. Fortunately, the validator allows JSONP, so we don't have to worry about same-origin security. Unfortunately, the documentation wiki is wrong about exactly what is returned, so it took some experimentation to get it to work.
The W3C validator has a validator and does have a web service, but the only documented output format is SOAP. output=json
works to produce JSON, but I can't find a way to get JSONP.
Leave a Reply