5

I have a HTML comment outside of the DOM root node which I need to read:

<html>
   ... other stuff
</html>
<!-- The comment I want to read -->

Can I do this with JavaScript somehow?

1
  • 1
    They're actually HTML comments Commented Sep 21, 2009 at 16:33

3 Answers 3

6

I did some testing on this and discovered that any HTML beyond </html> is appended to the <body> during parsing. So, in all browsers I tested (IE6/7, FF3, Opera10, Chrome2), the comment was accessible as document.body.lastChild and its contents can be retreived via document.body.lastChild.data.

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent work! I actually had an empty row at the end of my file so the document.body.lastChild.data was '\n', but by looking in firebug I see that the comment exists as a child node to body. Thanks!
This is not a generic solution.
Nice - using this technique with document.firstChild for comments before any other content.
2

The DOM standard for the #document node (the logical root of a DOM document) allows exactly one element node (i.e. one <HTML>), but many comment nodes (and other, more esoteric node types). As such, we should expect

document.lastChild

to be the comment node you want in this instance. Oddly, this does not work (at least in Safari 4).

Putting a comment node before the HTML node is fine in my tests. In this case, document.childNodes.length is 2, and document.firstChild is the comment. Putting a comment after the HTML node seems to be inserted in the wrong place in the DOM - document.childNodes.length remains at 1, and the node in the DOM inspector is seen as the last child of the BODY node. However, I can't find that node using the DOM API at all!

This seems to be an oddity in (at least) the Safari implementation of DOM. A quick test using Firefox shows that it can't even find a comment node if it's the first entry in the document!

Edit: OK, as per Aaron's comment and some more pondering, it's likely that the parser for the page is just discarding the nodes during parsing. I'm no longer certain this is a bug, but I can't find anything in the (XML) spec that allows parsers to discard entire comment nodes - only their textual content.

3 Comments

The standard allows several comments but most XML/HTML parsers don't put them in the DOM. It's not a bug since the standard doesn't require them to do it.
those comments are mainly for the parser ... doctype etc?
A doctype is not a comment (and uses different syntax). Comments are purely for humans, but should be preserved during parsing, imo.
0

There is a jQuery plugin for this problem: http://www.bennadel.com/index.cfm?dax=blog:1563.view

It seems that

objChildNode.nodeType === 8

is the point!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.