The HTML DOM doctype property returns the DTD (Document Type Declaration) that are associated with the current HTML document. It is a read-only property. It returns the doctype name as the DocumentType object. It can return null if no DTD is specified for the given document.
Syntax
Following is the syntax for doctype property −
document.doctype
Example
Let us look at an example for the doctype property −
<!DOCTYPE html> <html> <body> <h1>doctype property example</h1> <button onclick="getDoctype()">GET DOCTYPE</button> <p id="Sample"></p> <script> function getDoctype() { var doc = document.doctype.name; document.getElementById("Sample").innerHTML ="The doctype for this HTML document is: "+doc; } </script> </body> </html>
Output
This will produce the following output −
On clicking the GET DOCTYPE button −
We have first set the DTD(Document Type Defination) to HTML for our HTML document. This will ensure that the document is rendered as an HTML document −
<!DOCTYPE html>
We have then created a button GET DOCTYPE that will execute the function getDoctype() when clicked by the user −
<button onclick="getDoctype()">GET DOCTYPE</button>
The getDoctype() method uses the document’s doctype property name value and assigns it to the variable doc. The variable doc is then displayed in the paragraph with id “Sample” and its innerHTML property is set to the intended text −
function getDoctype() { var doc = document.doctype.name; document.getElementById("Sample").innerHTML ="The doctype for this HTML document is: "+doc; }