I wrote a very simple JavaScript script to traverse the DOM of a page. Here's the full page:
<html>
<head>
<title>DOM Traversal</title>
<script>
// Traversing the DOM tree
"use strict";
//var node = document.documentElement; //works
var node = document.body; // does not work
while(node) {
console.log(node);
node = node.lastChild;
}
</script>
</head>
<body>
<h1>Sample H1</h1>
<div id="text">
<p>Sample paragraph</p>
</div>
</body>
</html>
As you can see in the comments, when I set node
to document.documentElement
, the traversal works, but not with document.body
. Why is that? I'm on Chrome 35.0.xxxx, by the way.
` tag, to ensure the html elements are loaded.
– Praveen Jun 03 '14 at 12:03