getElementsByClassName() weirdness
As I’m working on the next edition of Professional JavaScript, I’ve been updating rewriting a large amount of it to include more of the vendor-specific JavaScript extensions. Today I took a look at getElementsByClassName(), which is implemented in Firefox 3 Beta 2 and the latest builds of WebKit.
This method is specified in HTML 5 as returning a NodeList of results. Ugh. Yet another performance cost to yet another DOM method. Groaning aside, it appears that the implementation in Firefox 3 Beta 2 actually returns an Array instead of a NodeList. I did some digging to figure out if maybe they had changed NodeList so that it inherited from Array, but this is not the case. WebKit, on the other hand, does return a NodeList of results.
And just so everyone knows, it looks like Opera 9.5 is also supporting getElementsByClassName(). It also correctly returns a NodeList. Looks Firefox is the only one behind the times on this one, but it is still in beta so hopefully it will be fixed.
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.
Both comments and pings are currently closed.




10 Comments
Out of curiousity did you try what happens in FFX if the dom is changed? Maybe they’re merely returning the wrong typename, but implementing the "correct" dynamic results.
That said, yes i agree having these return NodeLists seems needlessly expensive — but maybe there are common use cases i have thought of?
Oliver on January 13th, 2008 at 10:47 pm
Good find. Wouldn’t it be a good idea to report this as a bug to the mozilla guys?
Sebastian Werner on January 14th, 2008 at 2:14 am
@Oliver – I didn’t take the time to do the test you suggested, however I checked the value using instanceof, checked the constructor property, and also tested for array methods like sort(). All of them indicated that the returned value was an array.
@Sebastian – It would be a good idea, except that I’d have to sign up for an account and truth be told, I’m just way too lazy for that.
I’ll ping our contact at work when I get in tomorrow.
Nicholas C. Zakas on January 14th, 2008 at 2:38 am
I am using a latest trunk build of Firefox 3 and I am getting a [object HTMLCollection] back that is not instanceof Array.
José Jeria on January 14th, 2008 at 4:06 am
Ah, perhaps it’s already fixed then. Beta 2 is definitely returning an array and I didn’t see any open bugs related to this issue.
Nicholas C. Zakas on January 14th, 2008 at 12:57 pm
Neither did I when I searched bugzilla.
How about posting the code you tested with?
José Jeria on January 14th, 2008 at 1:44 pm
Nothing fancy:
document.getElementsByClassName("blah") instanceof Array
document.getElementsByClassName("blah") instanceof NodeList
document.getElementsByClassName("blah").constructor == Array
document.getElementsByClassName("blah").sort instanceof Function
Nicholas C. Zakas on January 14th, 2008 at 10:39 pm
Tested what you posted and they are all equals to false.
José Jeria on January 15th, 2008 at 4:03 am
Awesome! Then that means it’s already been fixed. Thanks for looking into this, José.
Nicholas C. Zakas on January 15th, 2008 at 12:53 pm
Nicholas, could you consider write something about talking to Flash from JavaScript in the next edition of Professional JavaScript. Thx
Robert on January 17th, 2008 at 4:18 pm
Comments are automatically closed after 14 days.