Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine doctype if none was set in the document?

Suppose, as an example, the following exact test.html document, saved in UTF-8 encoding:

<html>
<head>
    <title>Test</title>
</head>
<body>
    Test document.
</body>
</html>

How can I determine in IE11 and Chrome latest what the browser decided to guess for a doctype, if any? (Note that the document above is merely an example, I'm looking for a method to determine doctype as assumed by the browser, not an answer to this instance. (Teach me how to fish!))

I searched and found this related question where answers mention document.doctype, but that's null in Chrome and undefined in IE11. I also tried all sorts of other searches, for example this one, but this only results in posts about setting a correct doctype.

I've gone through the developer tools of both Chrome and IE11 but didn't find it (perhaps just didn't see it).

I've carefully reviewed all suggested duplicate questions on Stack Overflow, as well as the "Similar Questions" in the side bar, but none quite answer my question.

Bottom line: can JavaScript (preferred) or the browser's developer tools be used to find out what document mode was assumed? Or are there specs or known rules for this?

like image 991
Jeroen Avatar asked Sep 14 '15 11:09

Jeroen


1 Answers

Short answer: If no doctype is specified, no doctype is available.


According to this answer, browsers don't assume a doctype but rather enter a "quirks mode":

The usual method of setting quirks mode is not not include a doctype, or include content before the doctype.

(Read the full answer for a comparison of the three basic modes.)

You can find an overview over the different doctypes and which modes they trigger on Wikipedia. It confirms that all browsers enter a quirks mode if no doctype is present.

This thread contains information about how to check whether a browser is in quirks mode, primarily by checking document.compatMode.

like image 184
Tobias Avatar answered Sep 16 '22 21:09

Tobias