Optimized selector function

This commit is contained in:
Timothy Warren 2011-07-05 11:22:23 -04:00
parent 48ee0bde52
commit c8d8d16a7e

19
kis.js
View File

@ -31,8 +31,23 @@
*/ */
$ = function (a) $ = function (a)
{ {
var x;
if (typeof a !== "string"){ return a;} if (typeof a !== "string"){ return a;}
var x = document.querySelectorAll(a);
//Pick the quickest method for each kind of selector
if(a.match(/^#([\w\-]+$)/))
{
return document.getElementById(a.split('#')[1]);
}
else if(a.match(/^([\w\-]+)$/))
{
x = document.getElementsByTagName(a);
}
else
{
x = document.querySelectorAll(a);
}
return (x.length === 1) ? x[0] : x; return (x.length === 1) ? x[0] : x;
}; };
@ -48,7 +63,7 @@
var support = { var support = {
attachEvent: typeof window.attachEvent === "function", attachEvent: typeof window.attachEvent === "function",
addEventListener: typeof window.addEventListener === "function", addEventListener: typeof window.addEventListener === "function",
querySelector: typeof document.querySelectorAll === "function", querySelector: typeof document.querySelectorAll === "function"
}; };
$_.support = support; $_.support = support;