views:

293

answers:

3

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?

A: 

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!

powtac
+2  A: 

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.

Adam Wright
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.
Aaron Digulla
those comments are mainly for the parser ... doctype etc?
n00b32
A doctype is not a comment (and uses different syntax). Comments are purely for humans, but *should* be preserved during parsing, imo.
Adam Wright
+4  A: 

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.

J-P
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!
Tomas Forsman
This is not a generic solution.
powtac