HTML is a subset of SGML.
XHTML is a subset of XML.
Both use separate parsers.
Presuming an HTML document is correctly served as text/html
and an XHTML application is correctly served as application/xhtml+xml
is it possible to detect which parser is used to render the page and if so how?
I do understand exactly what I am asking. Please do not insist on asking why I want to do this.
I'd rather not receive answers suggesting that I do not use one language or the other. This is to avoid debate and help produce an answer I can use.
[This is a replacement for my original answer. My original idea was to exploit differences in the behaviour of innerHTML. Although it worked fine in IE9, Firefox and Chrome, it turned out that it failed in Opera, which appears to use an HTML parser for innerHTML even for pages served as
application/xhtml+xml
]There's not too many ways to tell XML documents apart from HTML documents. One way however, is to exploit the case handling differences between HTML and XML.
In particular, the behaviour of Element.tagName differs. In an HTML parsed document, the element name will be coerced to upper case for
tagName
whereas in an XML parsed document it won't be. So we can testdocument.createElement("div").tagName == "DIV"
which will give a different result depending on how the document was parsed.See this test case:
See it in action: