views:

19

answers:

1
    for (i=1; i<=4; i++) {
        try {
           timer = document.getElementById("timer"+i).parentNode.parentNode.parentNode.childNodes[1].childNodes[0].childNodes[0].className;
        }
        catch(e) {
           FM_log("aguardaReforcos()", "ERRO - timer"+i);
        }

...

I have to it this way with try because otherwise I get a crash when it doesn´t find document.getElementById("timer"+i).parentNode.parentNode.parentNode.childNodes[1].childNodes[0].childNodes[0].className

is there another way of preventing this type of crash?

+2  A: 

You can use each of the properties in turn without causing a crash, but the code will of course contain a whole lot of tests:

var timer = document.getElementById("timer"+i);
if (timer) {
  timer = timer.parentNode;
  if (timer) {
    timer = parentNode;
    if (timer) {
      // and so on...
    }
  }
}
Guffa
I would suggest using something like jQuery or Prototype if you're doing this kind of selection. They provide much cleaner and more powerful selectors. Using jQuery in you're page is as simple as adding: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
Java Drinker
it seems easier just the way it is now.
Fernando SBS