javascript - Why is my browser forcing the script tag inside the body tag? -
i'm trying learn javascript beginning understand it.
what i'm trying here output nodetypes of each element found in <body>
tag. understand there invisible texts between <body>
's child elements unknown reason, makes output
3 1 3 1
i put <script>
tag outside <body>
tag, it's still being counted in for
loop, resulted last digit of 1
in 3 1 3 1
loop sequence. why? why <script>
tag being forced inside <body>
tag browser?
<html> <body id = "bodytest"> <p>some text</p> </body> <script type="text/javascript"> var c = document.body.childnodes; var txt = ""; for(var k = 0; k < c.length; k++) { txt += c[k].nodetype + " "; console.log(txt); console.log(c[k].nodename); } alert(txt); </script> </html>
here code using.
<html> <body id = "bodytest"> <p>some text</p> </body> <script type="text/javascript"> // code above </script> </html>
that's not valid html. <html>
tag can contain <head>
, <body>
tags, not <script>
.
see the specification:
permitted contents
1 head element, followed 1 body element
when browser encounters broken html, tries fix it. in case, means treating <script>
tag though in <body>
.
Comments
Post a Comment