0

The end result I'm after is a JavaScript array containing a list of tag names that are used in the HTML document eg:

div, span, section, h1, h2, p, etc...

I want the list to be distinct and I'm not interested in tags within the <head> of the document (but they can be there if it's a performance hog to exclude them).

This has to work in IE 6, 7, & 8 and I don't want to use jquery.

What would be the most efficient way of doing this?

2
  • Why do you not want to use jQuery? It supports IE 6. Commented Oct 18, 2012 at 5:07
  • I don't want jQuery to be a dependancy. Commented Oct 18, 2012 at 6:30

4 Answers 4

2

What you're looking for is document.all.tagName At the top of my head, a for loop like this should do it (providing that you're gonna filter the tags you don't want on that list)

for(i = 0; i < document.all.length; i++)
{
  console.log(document.all[i].tagName);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, document.all is soooo cross browser. Not! :-) Use getElementsByTagName('*') instead and you get all browsers, not juse IE and friends.
1

Here is a cross-browser solution:

var tags = {};  // maintains object of tags on the page and their count
var recurse = function(el) {
    // if element node
    if(el.nodeType == 1) {
        if(!tags[el.tagName])
            tags[el.tagName] = 0;
        tags[el.tagName]++;    
    }   
    // recurse through the children
    for(var i = 0, children = el.childNodes, len = children.length; i < len; i++) {
        recurse(children[i]);
    }
}

recurse(document);


// if you want just what's in the body(included)
var bodies = document.getElementsByTagName("body");
for(var i = 0; i < bodies.length; i++)
    recurse(bodies[i]);

Comments

0

To get a unique list of tagnames in a document as an array that works in all browsers back to IE 6 and equivalent:

function listTagNames() {
  var el, els = document.body.getElementsByTagName('*');
  var tagCache = {};
  var tagname, tagnames = [];

  for (var i=0, iLen=els.length; i<iLen; i++) {
    tagname = els[i].tagName.toLowerCase();

    if ( !(tagname in tagCache) ) {
      tagCache[tagname] = tagname;
      tagnames.push(tagname);
    }  
  }
  return tagnames;
}

If you think there might be an inherited object property that is the same as a tag name, use a propertyIsEnumerable test:

      if (!tagCache.propertyIsEnumerable(tagname)) { 

so it will work even in Safari < 2.

Comments

0

Get all tagnames in the document, unique, crossbrowser, plain js:

var els = document.getElementsByTagName('*'), tags = [], tmp = {}
for (var i=0;i<els.length;i+=1){
 if (!(els[i].tagName in tmp)){
   tags.push(els[i].tagName);
   tmp[els[i].tagName] = 1;
 }
}

use

if (!(els[i].tagName in tmp) 
     && !/head/i.test(els[i].parentNode.tagName) 
     && !/html|head/i.test(els[i].tagName))

to exclude the <head>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.