[Javascript] DOM navigation

Paul Novitski paul at novitskisoftware.com
Wed Jun 2 13:42:03 CDT 2004


At 09:51 AM 6/2/2004, Gee, David wrote:
>  document.getElementById('myDiv').getElementsByTagName("TD")[0]


David, I realize that you're merely illustrating the path through the DOM, 
but I am moved to say that this single-statement style of programming makes 
me nervous because it doesn't fail gracefully.  If "myDiv" isn't found, the 
expression will generate a run-time error.  In my more conservative moments 
I'd write this more safely as:

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

Cheers,
Paul 




More information about the Javascript mailing list