Noob_Number_1 Noob_Number_1 - 1 year ago 67
Javascript Question

Why there's a text node between head and body nodes in DOM tree?

I'm messing around with DOM tree. In particular with this HTML Code

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<!-- <link rel="stylesheet" href="style.css"> -->
<script src=""></script>
<script src="script.js"></script>
<input type="text" name="movie-title" size="50"
placeholder="Enter movie title..." />
<input type="submit" name="search" value="Search"/>

and there's something I'm not able to figure out. When I check node children (
) I always get three children instead of the two I was expecting to:

  1. <head>

  2. #text

  3. <body>

What is this
node? In JSFiddle I'm getting same result.

Thanks in advance.

enter image description here

Answer Source

Look at the textContent or other similar properties - it's a line feed and a few spaces, which is exactly between your </head> and <body>.

So there's nothing "useful" between head and body, but that space is still there.


<div><a href="#">Some Link</a> Some Text <a href="#">Another Link</a></div>

This would have three nodes - the first link, the text, and the second link. Same deal, except your "text" is just a bunch of whitespace.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download