Open In App

HTML DOM createDocument() Method

Last Updated : 12 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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;">  
     GeeksforGeeks
    </h1> 
    <p id="a"> 
    HTML | DOM createDocument() method
    </p>

    <button onclick = "Geeks()">
    Click Here
    </button>
    <script> 
        function Geeks(){
            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

Next Article

Similar Reads