Question

The following source code alerts the following results:

Internet Explorer 7: 29
Firefox 3.0.3: 37 (correct)
Safari 3.0.4 (523.12.9): 38
Google Chrome 0.3.154.9: 38

Please ignore the following facts:

  • Webkit (Safari/Chrome) browsers insert an extra text node at the end of the body tag
  • Internet Explorer doesn't have new lines in their whitespace nodes, like they should.
  • Internet Explorer has no beginning whitespace node (there is obvious whitespace before the <form> tag, but no text node to match)

Of the tags in the test page, the following tags have no whitespace text nodes inserted in the DOM after them: form, input[@radio], div, span, table, ul, a.

My question is: What is it about these nodes that makes them the exception in Internet Explorer? Why is whitespace not inserted after these nodes, and is inserted in the others?

This behavior is the same if you switch the tag order, switch the doctype to XHTML (while still maintaining standards mode).

Here's a link that gives a little background information, but no ideal solution. There might not be a solution to this problem, I'm just curious about the behavior.

Thanks Internet, Zach

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
        function countNodes()
        {
            alert(document.getElementsByTagName('body')[0].childNodes.length);
        }
        </script>
    </head>
    <body onload="countNodes()">
        <form></form>
        <input type="submit"/>
        <input type="reset"/>
        <input type="button"/>
        <input type="text"/>
        <input type="password"/>
        <input type="file"/>
        <input type="hidden"/>
        <input type="checkbox"/>
        <input type="radio"/>
        <button></button>
        <select></select>
        <textarea></textarea>
        <div></div>
        <span></span>
        <table></table>
        <ul></ul>
        <a></a>
    </body>
</html>
Was it helpful?

Solution

IE tries to be helpful and hides text nodes that contain only whitespace.

In the following:

<p>
<input>
</p>

W3C DOM spec says that <p> has 3 child nodes ("\n", <input> and "\n"), IE will pretend there's only one.

The solution is to skip text nodes in all browsers:

var node = element.firstChild;
while(node && node.nodeType == 3) node = node.nextSibling;

Popular JS frameworks have functions for such things.

OTHER TIPS

Well... I'd say the reason it that it is IE. I don't think the programers had a specific intention to do it that way.

I'm guessing that the table tag is different between browsers.

e.g. which nodes does the default table auto-magically contain?

<table>
  <thead>
  </thead>
  <tbody>
  </tbody>
  <tfoot>
  </tfoot>
</table>

Why not just try walking the DOM and see what each browser thinks that the document contains?

IE does a lot of "optimization" of the DOM. To get an impression of what this might look like, "Select all", "Copy" in IE, and then "Paste Alternate" in Visual Studio, You get the following:

<INPUT value="Submit Query" type=submit> 
<INPUT value=Reset type=reset> 
<INPUT type=button> 
<INPUT type=text> 
<INPUT value="" type=password> 
<INPUT type=file> 
<INPUT type=hidden>
<INPUT type=checkbox>
<INPUT type=radio>
<BUTTON type=submit></BUTTON> 
<SELECT></SELECT>
<TEXTAREA></TEXTAREA> 

So it nukes some of the empty tags and adds some default attributes.

Zachleat, I think I've found a solution to the problem. (I've taken liberty of moving the form elements into the form tag.)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!--REF http://stackoverflow.com/questions/281443/inconsistent-whitespace-text-nodes-in-internet-explorer -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
    function countNodes()
    { alert(document.getElementsByTagName('form')[0].childNodes.length);
    };
</script>
</head>
<body onload="countNodes()">
  <form
    ><input type="submit"/
    ><input type="reset"/
    ><input type="button"/
    ><input type="text"/
    ><input type="password"/
    ><input type="file"/
    ><input type="hidden"/
    ><input type="checkbox"/
    ><input type="radio"/
    ><button></button
    ><select></select
    ><textarea></textarea
    ><div></div
    ><span></span
    ><table></table
    ><ul></ul
    ><a></a
  ></form>
</body>
</html>

As you can see, I've split & strapped the closing gt (greater-than) brackets so they hug to the next tag, thus ensuring there are no ambiguous whitespaces.

It was a nice surprise that it worked for all (4 desktop) browsers tried so far, all reporting the same number of DOM nodes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top