Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reliabilty of 'isConnected' field in dom node

domNode.isConnected is a flag which is available in chrome. It shows whether domNode is part of the document.
Is it cross browser compatible?
If not is there any efficient alternative for this for other browsers? Please provide link for any available documentation.

like image 488
rishabh dev Avatar asked Jun 02 '16 09:06

rishabh dev


2 Answers

It is not supported, but very easy to polyfill.

(function (supported){
  if (supported) return;
  Object.defineProperty(window.Node.prototype, 'isConnected', {get})
  function get() {
    return document.contains(this);
  }
})('isConnected' in window.Node.prototype);
like image 177
Joel Cox Avatar answered Sep 20 '22 06:09

Joel Cox


A quick test shows Firefox doesn't support this property. So the answer is no.

var input = document.getElementById('input');
alert(input.isConnected);
<input type="text" id="input">

https://jsfiddle.net/hu08awn0/

like image 38
Pimmol Avatar answered Sep 24 '22 06:09

Pimmol