KBD

Keith Devens .com

Saturday, May 17, 2008 Flag waving
I meant what I said, and I said what I meant. An elephant's faithful, one hundred percent! – Horton (Horton Hears a Who, Dr. Seuss)
← Guides | World of Warcraft ProWindows PowerShell →

Daily link icon Tuesday, April 15, 2008

getElementsByClass function

Javascript getElementsByClass function. I know there's one built into prototype, but I didn't want to include the whole library since I wasn't already using it. This seems to work.

← Guides | World of Warcraft ProWindows PowerShell →

Comments XML gif

Revence (http://freeshells.ch/~revence) wrote:

That getElementsByClass is severely flawed, I think. It uses a regular expression (which may run you into, at the very least, O(n log(n)) comparisons). That bites rather hard, if you have many elements - as you will.
A simple comparison would suffice, I think. But that's not the real sin, here.
The real sin is not checking if getElementsByClass is already implemented. So, if you drop this in, you're in trouble. Say, Safari Webkit has one in-built, these days. You'd, in this case, override a native implementation (which is, of course, faster and more-correct) for this one.

Plus, that code is horribly very non-JavaScript. That guy is probably a Java or C/C++ programmer, originally. I'll be back with code, if it suits you. (I'll write it, anyway, and check back to see if posting it would still be pertinent.) There is a stunningly-good implementation I saw for one of the libs. Prototype, I think.

∴ Revence | 15-Apr-2008 11:42pm est | http://freeshells.ch/~revence | #10638

Revence (http://freeshells.ch/~revence) wrote:

Okay, I'm back with the code. I saw one John Resig wrote, and it is here.

My own is here. Frankly, I wonder why that one of Dustin Diaz gets mentioned at all. It looks horrible to me, especially that j++. Eeewww. Also, Array.length is O(1), so why clutter with a var for a var? Then the repeated checking if "equals null". This was a Java programmer, I can bet. And when will people learn that regular expressions are not to be used where simple comparisons would work?
(I didn't use regexen in mine, because I've never needed 'em. I see that many versions have them in there, but ... isn't this an example of people forgetting that YAGNI?)

function getElementsByClass(cls, node, tag)
{
    if(document.getElementsByClass)
        return document.getElementsByClass(cls, node, tag);
    if(! node) node = document;
    if(! tag)  tag  = '*';
    var them = node.getElementsByTagName(tag),
        rez  = [];
    for(var notI = 0; notI < them.length; ++notI)
        if(them[notI].className == cls) rez.push(them[notI]);
    return rez;
}

When I still coded JavaScript, this function worked for me. (It should work, though not tested ... so it shouldn't. Smiley)

∴ Revence | 16-Apr-2008 12:15am est | http://freeshells.ch/~revence | #10639

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

May 2008
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Maps of Iraq

my husband is in Scania too..he​says it's not too bad..he's been at​worse...

Cristy: May 16, 3:54pm

"IMDB for music"

IMDB for Music? It looks to be a​couple of years old...​http://MusicTell.co...

Ken Empie: May 14, 9:57pm

Generated in about 0.177s.

(Used 8 db queries)