You are here: Reference > JavaScript > client-side > HTML DOM > methods > normalize

normalize method

Browser support:
Puts the subtree that belongs to the current node into a 'normalized' form.
The normalize method joins adjacent text nodes into a single text node and removes empty text nodes. Note: in Internet Explorer, the normalize method does not remove empty text nodes. See the example below for details.

Syntax:

object.normalize ( );
You can find the related objects in the Supported by objects section below.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the normalize method.
<head>
    <script type="text/javascript">
        function NormalizeAdjacentTextNodes () {
            var textContainer = document.getElementById ("textContainer");

            var textNode1 = document.createTextNode ("First text node.");
            var textNode2 = document.createTextNode ("Second text node.");

            textContainer.appendChild (textNode1);
            textContainer.appendChild (textNode2);

            alert ("Before normalizing, the textContainer element contains " + textContainer.childNodes.length + " text nodes.");

            textContainer.normalize ();

            alert ("After normalizing, the textContainer element contains " + textContainer.childNodes.length + " text node.");
            alert ("The contents of the text node are:\n" + textContainer.firstChild.data);

            textContainer.removeChild (textContainer.firstChild);
        }

        function NormalizeEmptyTextNodes () {
            var textContainer = document.getElementById ("textContainer");

            var emptyTextNode = document.createTextNode ("");
            textContainer.appendChild (emptyTextNode);

            textContainer.normalize ();

            if (textContainer.firstChild) {     // Internet Explorer
                alert ("The normalize method does not remove empty text nodes in your browser!");
                textContainer.removeChild (textContainer.firstChild);
            }
            else {
                alert ("The normalize method removes empty text nodes in your browser.");
            }
        }
    </script>
</head>
<body>
    <div id="textContainer"></div>

    <button onclick="NormalizeAdjacentTextNodes ();">Test the normalize method for adjacent text nodes</button>
    <br /><br />
    <button onclick="NormalizeEmptyTextNodes ();">Test the normalize method for an empty text node</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content