Reputation: 57916
I have an app that receives some HTML and sometimes it doesn't contain a doctype it just starts off with the <html>
tag.
I can return an error to the user and ask them to define a doctype but I would rather go the extra mile (if possible) of somehow determining an appropriate doc type by looking at the HTML code.
Is this possible? With JS? PHP?
Upvotes: 2
Views: 149
Reputation: 15938
You could check some basics of the code.
If you have this three answers you can differ between
I guess this is easier than using the api.
Upvotes: 0
Reputation: 943108
The simplest option would probably be to validate the document multiple times, prepending a different Doctype each time.
You could then assume that which ever Doctype resulted in the fewest errors was the one to use.
The W3C Markup Validation Service has an API and you can download and install a copy locally for better performance (and to avoid hammering a free service provided by a third party).
Upvotes: 4