11

Passing DOM elements to WebWorkers gets tricky since all references to the DOM are 'lost'. I need to check objects that are passed before the WebWorker's message is sent.

What is the fastest way to check if an instance of an object is a DOM-element OR/AND part of the DOM tree, OR has 'children' that contain any references to the DOM tree?

piece of the usage:

var a = new SharedWorker("bigdatahandler.js");   
a.postMessage(s);

s //<--should not be a DOM object
Jonas
  • 121,568
  • 97
  • 310
  • 388
Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
  • This is already answered here: http://stackoverflow.com/questions/4754088/how-to-check-if-object-is-a-dom-element/4754104#4754104 ;) – Martin Jespersen Jan 22 '11 at 13:03

3 Answers3

10

To check whether an object is an Element instance, use instanceof:

s instanceof Element

And to check its owner document, use ownerDocument:

s.ownerDocument == document
Gumbo
  • 643,351
  • 109
  • 780
  • 844
8

To check if it's an element I think obj.nodeName is your best bet.

var a = new SharedWorker("bigdatahandler.js");   
if (!s.nodeName) {
    a.postMessage(s);
}

You can also check s instanceof Element, because you don't need to support IE I guess :)

To check if it's part of the DOM:

function inDOM(elem) {
  do {
      if (elem == document.documentElement) {
         return true;
      }
  } while (elem = elem.parentNode)
  return false;
}​
gblazex
  • 49,155
  • 12
  • 98
  • 91
  • 1
    ...and to check if it has any "children" that are DOM elements, you'll need to use recursive descent on all of the properties of the given item (if it's an object). – T.J. Crowder Jan 22 '11 at 12:24
  • @TJ - He said *`"has 'children' that contain any references to the DOM tree"`*. If the element is not part of the DOM its children cannot contain references to the DOM, easy as that :) – gblazex Jan 22 '11 at 12:26
  • @gal Note, the OP didn't say HTML DOM element, just DOM element. (also, inside that long nick of yours, where's the name? :) Is @gal OK?) – Šime Vidas Jan 22 '11 at 13:04
  • @Šime Vidas - Balazs is my first name. The nick is a shorthand of my full name, but the two parts are in reversed order in my country :) – gblazex Jan 22 '11 at 13:55
  • `!s.nodeType` doesn't tell you if something's an element. `s.nodeType === s.ELEMENT_NODE` does. Anyways, he doesn't even want that, he wants to tell if something's a `Node`. – Eli Grey Jan 22 '11 at 16:38
  • Also, if you want to check if something is in the current document, you *should* use `s.ownerDocument === document`. What if something is a child of `` or ``? Your code fails in that situation. – Eli Grey Jan 22 '11 at 16:40
  • @Eli - Your statements are correct, but they are only different because of **different assumptions**. I guessed he doesn't want to pass anything other than nodes and normal objects anyways. Then he just wants to differentiate. My answer is correct in that matter. – gblazex Jan 22 '11 at 17:28
  • Ups, only one thing. I wanted to write `nodeName` not `nodeType`. Thanks for remembering me :) As for `ownerDocument` I wasn't sure what browsers support that. Now I see that everything above IE 5.5+ is ok. :) – gblazex Jan 22 '11 at 17:34
  • No good. What if the element is created but not yet added to the DOM? Then this method would fail. – ChrisRich Aug 17 '12 at 04:46
  • @galambalazs - Sorry (DOM newbie alert!) **`"If the element is not part of the DOM..."`**: I could see that not all **`Objects`** will be **`part of the DOM`**. But what sort of an **`Element`** will ***Not*** be part of the DOM? (I'm thinking [perhaps incorrectly] that **`Element`** means an **`HTML Element`** like **`
    `, ``, ``**, etc...).
    – Kevin Fegan Jan 24 '14 at 17:55
1

Check s instanceof Node. Every DOM object is a Node.

Eli Grey
  • 35,104
  • 14
  • 75
  • 93
  • Is FileReader a DOM element? It doesn't pass this test. – jayarjo Jul 09 '11 at 14:01
  • FileReader is *not* a DOM object, but you are correct that I screwed up. `NodeLists`, `DOMStrings`, and a couple other list structures are not `Node`s, but are DOM objects. – Eli Grey Jul 09 '11 at 16:44
  • 2
    Down-voting for acknowledged but uncorrected error. I'll revert when it's corrected. – danorton Feb 02 '12 at 23:51