Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a HTML comment located outside of <html> using JavaScript?

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?

like image 732
Tomas F Avatar asked Sep 21 '09 14:09

Tomas F


2 Answers

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.

like image 195
James Avatar answered Oct 13 '22 00:10

James


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.

like image 30
Adam Wright Avatar answered Oct 12 '22 23:10

Adam Wright