Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript IE and getElementsByClassName problems [duplicate]

So first off I am learning JS and will not use a library until I know all I need to.

http://www.tombarrasso.com/search/

With that said why on earth does my code not work in < IE 7, Firefox < 3, etc? Safari 4 is perfect, Firefox 3.5 is great, etc. I am using the function getElementsByClassName from Robert Nyman.

It generates no errors in my IE 6 on OS X (through Wine). The Ajax request is fine, but there should be an initial div with id="one" and class="scroller", but there is not. I am just getting so frustrated with this.

Thanks,

  • Tom
like image 410
Tom Avatar asked Aug 15 '09 01:08

Tom


3 Answers

..or you can use this as a better solution...

if (typeof document.getElementsByClassName!='function') {
    document.getElementsByClassName = function() {
        var elms = document.getElementsByTagName('*');
        var ei = new Array();
        for (i=0;i<elms.length;i++) {
            if (elms[i].getAttribute('class')) {
                ecl = elms[i].getAttribute('class').split(' ');
                for (j=0;j<ecl.length;j++) {
                    if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                        ei.push(elms[i]);
                    }
                }
            } else if (elms[i].className) {
                ecl = elms[i].className.split(' ');
                for (j=0;j<ecl.length;j++) {
                    if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                        ei.push(elms[i]);
                    }
                }
            }
        }
        return ei;
    }
}
like image 176
gdarcan Avatar answered Nov 19 '22 17:11

gdarcan


Personally, I would just use jQuery, as it already has the browser-compatability problems you are running into.

It's all fine to learn it all; but when it comes to handling implementation compatability it's better to have it dealt with for you, IMHO. There are just some things I don't care about :P

like image 21
Noon Silk Avatar answered Nov 19 '22 16:11

Noon Silk


IE8 gives error -"Object doesn't support property or method" when we use getElementsByClassName in javascript. Try this instead.

if (document.getElementsByClassName) {
    var obj = document.getElementsByClassName('classA');
}
like image 2
Purni Avatar answered Nov 19 '22 16:11

Purni