Know if the DOM object is dead

I am working with a variable containing a webpage element, such as a button. However, sometimes I get the error "Unable to access dead object" because the page containing the item has changed since it was saved.

I would like to know how to check if an element is dead or not, I tried:

if(element) alert("Do something"); 

but it does not work properly.

+4
source share
2 answers

copied from How to check if an element exists in the visible DOM?

  var elementInDocument = function(element) { while (element = element.parentNode) { if (element == document) { return true; } } return false; } 

You can use it like:

 if(elementInDocument(element)) alert("Do something"); 
0
source
 //eval it in your mozilla-browser space var dc = content.document; content.document.location.reload(); setTimeout(function(){ try{ dc.parentNode; }catch(e){ if (e.message.indexOf(' dead ')!=-1){ alert('REALY DEAD!'); } } }, 1000); 

this is a test of the dead (in a try..catch block) in my moz extension projects.

0
source

Source: https://habr.com/ru/post/1498584/


All Articles