HTML | DOM normalizeDocument() Method

The normalizeDocument() method in HTML is used to normalize an HTML document by remove any empty text nodes if exists. After removing the empty nodes, it also merges all of the adjacent nodes present in the document.

For Example, consider the below element:

Element Geeks
    Text node: ""
    Text node: "Hello "
    Text node: "wor"
    Text node: "ld"

The above element after normalization will become “Hello world”.



Note:This method is not supported by any browser, but it works as DOM normalize() Method and Display the output.

Syntax:

geeknode.normalize()

Parameters:

  • The normalize() method does not require any parameter.
  • Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <meta http-equiv="Content-Type" 
         content="text/html; charset=utf-8" />
        <title>GeeksForGeeks | HTML DOM Normalize Method</title>
    </head>
      
    <body>
        <script type="text/javascript">
            var parent = document.createElement("div");
            parent.appendChild(document.createTextNode("Child 1 "));
            parent.appendChild(document.createTextNode("Child 2 "));
            parent.appendChild(document.createTextNode("Child 3"));
            
            // Here the length of parent's child nodes would be 3, 
            // you can check the same
            // using parent.childNodes.length. 
            alert('Before Normalize : child nodes length: ' + 
                  parent.childNodes.length);
            parent.normalize();
            document.body.appendChild(parent);
            
            // Now, parent.childNodes.length === 1
            // Content of all 3 childs nodes
            // are in one child node only.
            alert('After Normalize : child nodes length: ' + 
                  parent.childNodes.length);
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output :
    Before Normalize() call:

    After Normalize() call:

    Supported Browsers: The major browsers are not supported by DOM normalizeDocument() Method



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.