IE5's document.getElementsByTagName deficiency, sorted

I’m working on fixing up the XMLHTTPRequest shoppingcart so it will work in IE5 and IE6.

While debugging, I came across the annoyance that IE5 doesn’t properly return an array of all objects for document.getElementsByTagName('*'). I use that to find out what elements need to have JavaScript enhancements applied to them (see alistapart for PPK’s take on this).

Mentioned this on #linux, and Nargler offered this solution:

 function ie_getElementsByTagName(str) {
  // Map to the all collections
  if (str=="*")
   return document.all
  else
   return document.all.tags(str)
}
if(document.all)document.getElementsByTagName=ie_getElementsByTagName;

Of course, I had to tinker.

var supportsDom=(document.getElementsByTagName('*').length)?1:0;
if(!supportsDom && document.all){
 document.getElementsByTagName=function (str){
  return(str=='*')?document.all:document.all.tags(str);
 }
 supportsDom=1;
}

The bonus here is that document.getElementsByTagName is used in all cases where possible (IE6, for example), and you also get a supportsDom variable which you can use to decide whether to bother trying to add your enhancements.

Comments are closed.

%d bloggers like this: