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
To check if an element is connected or attached to the DOM or the document object ( or otherwise called the context ), you can use the isConnected property in the element's object in JavaScript. The isConnected element property returns boolean true if it connected to the DOM ( document object) and false if not.
The easiest way to find an HTML element in the DOM, is by using the element id.
The DOM id Property is used to set or return the id of an element i.e value of the Id Attribute. An ID should be different in a document. It is returned by using the document. getElementById() method.
HTMLElement : Element. HTMLElement represents an element in the HTML document tree. HTMLElement is the base type for HTMLDivElement, HTMLSpanElement, HTMLImageElement, and many others.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With