Search code examples
javascripthtmlchild-nodes

why script tag after body tag counts in childNodes


This is a simple HTML and JS code, but why scripts tag after body tag, count as childNodes of body!?

I am trying it in FF 35

<!DOCTYPE HTML>
<html>
    <head>
    </head>
    <body id='body'>This is only text node</body>
</html>

<script>
    var b;
</script>
<script>
    var b = document.getElementById('body');
    console.log(b.childNodes.item(3).textContent);
</script>

Solution

  • as others have pointed out the script tags must be inside the HTML tags otherwise browsers will try and fix it for you with bad consequences.