Before you edited the snippet to document.write before the document was completed, I created the code below - you many NOT use document.write after the document is completed but you CANNOT show the tags/nodes before they are rendered:
It seems there is some strange going's on with just getting the nodes - Chrome adds a newline after the head before the body
This shows this issue
You want to check out https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<p></p>
<script>
// https://developer.mozilla.org/en-US/docs/Web/API/NodeFilter/acceptNode
// https://developer.mozilla.org/en-US/docs/Web/API/Document/createTreeWalker
var nodeIterator = document.createNodeIterator(
// Node to use as root
document.querySelector('html'),
// Only consider nodes that are text nodes (nodeType 3)
NodeFilter.SHOW_ELEMENT,
// Object containing the function to use for the acceptNode method
// of the NodeFilter
{
acceptNode: function(node) {
// Logic to determine whether to accept, reject or skip node
// In this case, only accept nodes that have content
// other than whitespace
if (!/^\s*$/.test(node.data)) {
return NodeFilter.FILTER_ACCEPT;
}
}
},
false
);
// Show the content of every non-empty text node that is a child of root
var node;
while ((node = nodeIterator.nextNode())) {
console.log(node.tagName);
}
/* ----------- Older code ------------- */
/*
var rt = document.getRootNode();
console.log("Root", rt.nodeName + " "); //document
var ht = rt.firstChild;
console.log("Root's firstChild", ht.nodeName + " "); // html
var HTML = ht.nextSibling;
console.log("html's nextSibling", HTML.nodeName + " "); // HTML
var HEAD = HTML.firstChild;
console.log("Html's firstChild", HEAD.nodeName + " "); // HEAD
var newLine = HEAD.nextSibling;
var BODY = newLine.nextSibling;
console.log("newLine's nextSibling", BODY.nodeName + " "); // BODY
*/
</script>
</body>
</html>
` tag.
– trincot Mar 04 '19 at 08:33