HTML DOM createDocument() Method

The DOMImplementation createDocument() method is used to create and return a Document.

Syntax:

var doc = document.implementation.createDocument(namespaceURI, qualifiedNameStr, docType);

parameters:

  • namespaceURI: It is a DOMString containing the namespace URI of the document to be created, or null if the document doesn’t belong to one.
  • qualifiedNameStr: It is a DOMString containing the qualified name
  • docType (Optional): It Is the Document type of the document to be created, the default value is null.

Return Value: This function returns DOMDocument object on success.

Example: In this example, we will create a document using this method.

html




<!DOCTYPE HTML>
<html
<head>
    <meta charset="UTF-8">
    <title>createDocument() method</title>
</head>  
 
<body style="text-align:center;">
    <h1 style="color:green;"> 
     w3wiki
    </h1>
    <p id="a">
    HTML | DOM createDocument() method
    </p>
 
    <button onclick = "Beginner()">
    Click Here
    </button>
    <script>
        function Beginner(){
            var doc = document.implementation.createDocument (
'http://www.w3.org/1999/xhtml', 'html', null);
            var head = document.createElementNS(
'http://www.w3.org/1999/xhtml', 'head');
            head.setAttribute('id', 'headDoc');
            doc.documentElement.appendChild(head);
            var body = document.createElementNS(
'http://www.w3.org/1999/xhtml', 'body');
            body.setAttribute('id', 'bodyDoc');
            doc.documentElement.appendChild(body);
            console.log(doc)
        }
  </script>
</body>  
</html>


Output:

Before Button Click:

After Button Click:

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Firefox 1
  • Safari 1
  • Opera 12.1
  • Internet Explorer 9


Contact Us