December 2, 2000 - Extended Tag Extraction | WebReference

December 2, 2000 - Extended Tag Extraction

Yehuda Shiran December 2, 2000
Extended Tag Extraction
Tips: December 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Both Internet Explorer 5 and Netscape 6 claim to be W3C-standard compliant. One supporting evidence is their support of the getElementsByTagName() method, which extracts an array of elements of the same tag type. This method applies to any element. You can extract all tags in a whole document, or in any other elements such a DIV, or a P. The following handleAllTags() function extracts all FONT tags in this tip as well as all tags in the DIV container that wraps the following code listing. It prints these two counts:

function handleAllTags() {
var arrayOfDocFonts, arrayOfDivFonts;
  if (document.all || document.getElementById) {
    arrayOfDivFonts = foo.getElementsByTagName("font");
    arrayOfDocFonts = document.getElementsByTagName("font");
  else {
    document.write("Unrecognized Browser Detected");
  alert("Number of font tags in document and DIV are " + arrayOfDocFonts.length + " and " + 
      arrayOfDivFonts.length + ", respectively.");
// -->

Notice that the tags extracted from the DIV with ID="foo" are found by foo.getElementsByTagName("font"). All this tip's tags are extracted by document.getElementsByTagName("font"). This tip has been contributed by Thomas Ashe.