There are a lot of IE user's out there- enough so that unless yours is a hobby site, you need to support it.
You can speed up IE8 a little by giving it its own methods, but for the other IEs and older browsers you need to
change your code so you don't call a nonexistant method.
I define a branching method that uses an getElementsByClassName when available, or
loops through an element's descendents, looking at attributes, when not. Remember there can be more than one class of an object.
/* @classes: space separated string of class names
@pa:parent element reference
*/
document.getbyClass= function(classes, pa){
pa= pa && pa.nodeType== 1? pa: document;
if(pa.getElementsByClassName){
return pa.getElementsByClassName(classes);
}
var elems= [], c= classes.split(/ +/), L= c.length, tem, temc,
tags= pa.getElementsByTagName('*'), max= tags.length;
for(var i= 0, L= c.length; i< L; i++){
c[i]= RegExp('\\b'+c[i]+'\\b');
}
getbyClassloop:
while(max){
i= L;
tem= tags[--max];
temc= tem.className;
if(temc){
while(i){
if(!c[--i].test(temc)) continue getbyClassloop;
}
elems[elems.length]= tem;
}
}
return elems;
}
// A quicker method for IE8
(function(){
if(!document.getElementsByClassName){
try{
if(document.querySelectorAll && document.attachEvent){
var IE8class= function(classes){
var C= classes.split(' '), tem,
els= Array.from(this.querySelectorAll('.'+ C.shift()));
while(C.length && els.length){
tem= C.shift();
els= els.testEach(function(itm){
return itm.className.indexOf(tem)!= -1;
});
}
return els;
}
HTMLDocument.prototype.getElementsByClassName= IE8class;
Element.prototype.getElementsByClassName= IE8class;
return true;
}
}
catch(er){return false};
}
})()
/*
these 2 methods are used with the above... maybe you should let IE struggle along with its relatives in the tag sifting group..
*/
Array.prototype.testEach= function(fun){
var A= [], tem, L= this.length;
for(var i= 0; i< L; i++){
tem= this[i];
if((!fun && !!tem) || !!fun(tem)) A[A.length]= tem;
}
return A;
}
Array.from= function(what){
var L, A= [];
if(what){
L= what.length;
if(L){
while(L) A[--L]= what[L];
return A;
}
if(what.hasOwnProperty){
for(var p in what){
if(what.hasOwnProperty(p)) A[A.length]= what[p];
}
}
}
return A;
}