Detect XML or SGML parser used in JavaScript

2019-09-22 01:35发布

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.

1条回答
爷的心禁止访问
2楼-- · 2019-09-22 02:11

[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 test document.createElement("div").tagName == "DIV" which will give a different result depending on how the document was parsed.

See this test case:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>      
        <title>Test Case</title>
        <script>
            window.onload = function() {
              document.getElementById("result")
                .appendChild(document.createTextNode(
                  (document.createElement("div").tagName == "DIV") 
                    ? "HTML parser" : "XML parser"));
            }
        </script>
    </head>
    <body>
        <p id="result"></p>
    </body>
</html>

See it in action:

查看更多
登录 后发表回答