Here is a simple fiddle:
alert(document.doctype.valueOf())
It produces [object DocumentType]
, as expected.
However, when I hit F12 and just type document.doctype.valueOf()
in the JavaScript console (Chrome, IE11), I see:
<!DOCTYPE html>
Why is the discrepancy, and what property (if any) does it actually show in the console mode?
[UPDATE] When I mentioned that document.doctype.outerHTML
used to work until IE11 in the comments to @BlueSkies's answer, I was not quite correct. In my case, I host a WinForms version of IE WebBrowser
control in a C# app. I've just discovered it works like this, in IE11 too:
dynamic domDocument = webBrowser.Document.DomDocument;
// this shows '<!DOCTYPE html PUBLIC "" "">'
string doctype = domDocument.doctype.outerHTML;
MessageBox.Show(doctype);
// this shows 'undefined'
domDocument.parentWindow.execScript("alert(document.doctype.outerHTML)");
Apparently, it works from outside, but not from inside the page. Interesting, but unreliable. I guess I should not use document.doctype.outerHTML
even in this IE-based app.