Modern browsers are implementing a DOM standard architecture that represents a document in the same way regardless of the browser you are running. This means that the focus of script development will move away from a browser-centric view to a standards-centric approach. Using DOM will lead to you developing a much more portable script product.
One of the important "gateway" methods to this DOM model is getElementsByTagName(). This can be applied to a document or to an individual HTML element within it.
The getElementsByTagName() method returns a collection of elements that are children of the receiving element so long as they were instantiated by the specified tag name.
Many HTML tags create a container within which other tags are placed. <HTML> contains <HEAD> and <BODY> as its immediate children. A <BODY> tag might contain a <TABLE> which contains <TR> row elements, each of which has its own collection of <TD> elements. Within those table cells, further parent- child relationships are constructed.
The getElementsByTagName() method will walk down that parent-child tree structure and collect a reference to each Element object that was instantiated by the HTML tag name you specify. Then on return, it passes back a Collection object with them organized for easy access with the item() method or by array index.
Since you can apply this search to an individual HTML element, the traversal of the document tree can be very efficient because you only need to walk the portion local to the objects you want to find.
Netscape 6.0 is forgiving of non-existent tag names. It will instantiate an Element object and you can access it using this DOM method even though the tag is not a valid HTML item. MSIE does not do that. It won't parse incorrect tags into DOM objects and therefore this is not a portable workaround for hiding data in documents.
Beware that item() methods that are used to access individual members of a collection come in several varieties with different argument values. In the case of the MSIE Collection object, the method name is capitalised so you need to call Item() and not item() on those collections.
Prev | Home | Next |
Element.getAttributeNode() | Up | Element.getExpression() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |