I’m working on a Firefox extension, in that I have a node which I want to know that whether the node belongs to the html (I mean the node belongs to document.body elements like div, p, etc) or just the window like menu, toolbar, etc.
Is there a way to do it in JavaScript?
Sorry if it’s a stupid question, since I’m new to JavaScript. Let me know if there’s something unclear or ambiguous.
I’m very thankful for your responses. 🙂
The simplest way I know of is to follow the parent chain and see if you find document.documentElement or not.
In jQuery, you can use
jQuery.contains(document.documentElement, el)In YUI3, you can use
node.inDoc()Curiously, both the jQuery and YUI implementations don’t just follow the parent chain – they check for the existence of the
.contains(el)or.compareDocumentPosition(el)methods on the desired ancestor and use whichever of those is present.