4

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

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

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

but it doesn't work as expected.

makdad
  • 6,402
  • 3
  • 31
  • 56
user2302725
  • 463
  • 6
  • 20

2 Answers2

0

copied from How to check if 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");
Community
  • 1
  • 1
tobspr
  • 8,200
  • 5
  • 33
  • 46
0
//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);

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

Alexander Goncharov
  • 1,572
  • 17
  • 20