[Javascript] DOM navigation

Paul Novitski paul at novitskisoftware.com
Wed Jun 2 18:50:31 CDT 2004


>From: Paul Novitski
>var oDiv = document.getElementById("myDiv")
>          if (oDiv) var oCells = oDiv.getElementsByTagName("TD")
>          if (oCells && oCells.length > 0) var oCell = oCells[0]

At 11:54 AM 6/2/2004, Gee, David wrote:
>If you're only targeting more modern browsers, wrapping the statement in
>a try/catch block could be less cumbersome.

Sure.  I'm curious: in your work, do you use try/catch blocks in place of 
testing DOM functionality as you go?  E.g.:

         if (document.getElementById) oDiv = document.getElementById("myDiv")

I don't use try/catch a whole lot because I feel more in control if I test 
each step as I go along, but I'd be very interested to hear testimonials 
praising its virtues or cursing its foibles.


>Also, what would the case be for oDiv.getElementsByTagName("TD")
>returning a collection with length <= 0?

You're right: if oDiv.getElementsByTagName() returns an object that exists, 
it must be an array, even if of only one element.

Paul  




More information about the Javascript mailing list