Last active
October 12, 2015 08:08
-
-
Save obenjiro/3997394 to your computer and use it in GitHub Desktop.
Smallest getElementsByClassName polyfill (146 chars)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//A little bit buggy, but for me it's ok (work wrong if \t (tab) is used inside of className field) | |
function getElementsByClassPolyfill(className, array, space, index, item){ | |
index = 0, space = " ", className = space + className + space, array = []; | |
while(item = document.all[index++]) { | |
if((space + item.className + space).indexOf(className)+1) { | |
array.push(item); | |
} | |
} | |
return array; | |
} | |
//obfuscated (146 chars) | |
function getElementsByClassPolyfill(a,b,c,d,e){for(d=0,c=" ",a=c+a+c,b=[];e=document.all[d++];)(c+e.className+c).indexOf(a)+1&&b.push(e);return b} | |
//usage example | |
getElementsByClassPolyfill("test"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment