W3C Markup Validation with JavaScript
I was just reading up on the validator API over at the W3C site
and saw that when you call the validator URL, the results are also put in the HTTP
response headers. That's pretty cool, I thought, and remembered one of Simon Willison's small JSON-P tools called json-head.
What json-head does is let you do a HEAD request for an arbitrary URL.
It then returns the headers, optionally calling a specified callback function.
So, what I've done is glue these two together to create a small function that lets
you validate a URL with a single JavaScript function call.
Example usage:
validate("http://www.google.com/", function(response) { alert( "URL: " + response.url + "\n" + "Status: " + response.status + "\n" + "Errors: " + response.errors + "\n" + "Warnings: " + response.warnings + "\n" + "Recursion: " + response.recursion ); } );As you can see, the
validate()
function takes two arguments, the URL to validate and a callback function. The callback function is given a response object that carries the properties as seen in the example. You can also provide an optional third argument which is a function that will be called should something go wrong (only some errors are currently caught by this).
The meaning of the properties are as follows (taken from http://validator.w3.org/docs/api.html#http_headers, go there for more details):- status:
Valid
orInvalid
if validation was performed.
value will beAbort
if a fatal error (decoding, 404 not found, etc) was encountered and validation could not be performed - errors:
Number of Errors found during validation.
0
if no errors found. - warnings:
Number of Warnings found during validation.
0
if no errors found. - recursion:
Integer. Generally,
1
. More if recursively validating validation results.
Very cool idea, Jacob. Would you like to drop a line about it to the public list where the validator users and developers hang out?
November 29, 2008 at 5:59 PM Jacob Seidelinhttp://lists.w3.org/Archives/Public/www-validator/
Thanks!
Done. Thanks for the feedback, Olivier!
November 30, 2008 at 3:27 AM AnonymousHi Jacob, Is there any chance that this could submit HTML code rather than a URL for validation? I'm thinking it would be cool to see this validate code I was writing or viewing locally.
March 31, 2009 at 6:17 PM Jacob SeidelinKeep up the great work.
Tom.
@Anon: I can see how that would be nice, but I'm not sure it can be done. At least not with the approach I'm using (with json-head).
March 31, 2009 at 9:37 PM UnknownNice information, I really appreciate the way you presented.Thanks for sharing..
April 16, 2010 at 7:14 AM AnonymousOKAY, ITS THE GREAT VALIDATOR MAYBE
September 6, 2010 at 10:35 PM