var nodes = document.body.childNodes,
comments = [];
for (var i = 0; i < nodes.length; i++) {
// if the nodeType is 8
if (nodes[i].nodeType == 8) {
// this is a comment
comments.push(nodes[i]);
}
}
Let's say that the comments array is not empty, so the type of comments[0] is a Comment. That's great but how can you read its content?
This is what the W3C DOM-Level-1 spec says. It implements the Comment interface.
interface Comment : CharacterData {
};
This does not really help us but don't despair. It also implements the Node interface.
...
readonly attribute DOMString nodeName;
attribute DOMString nodeValue;
...
Yes, it's read-only but you can get the comment as a string by using its nodeValue.
comments[0].nodeValue
W3C DOM-Level 3 specifies something called textContent. This can also do the job, you can read about them here, textContent vs. innerText