Skip to content

Instantly share code, notes, and snippets.

@icek
Forked from ofca/$.3.js
Last active January 21, 2016 20:47
Show Gist options
  • Save icek/e359679fa58b0e0d0b1c to your computer and use it in GitHub Desktop.
Save icek/e359679fa58b0e0d0b1c to your computer and use it in GitHub Desktop.
// original ofca/$.3.js
// based on https://gist.github.com/Potfur/5576225 & https://github.com/james2doyle/saltjs
// more info: https://plus.google.com/109231487156400680487/posts/63eZzzrBSb6
// 2 bytes less than original ;)
// V1 - 160b
window.$ = function(s,c) {
c = {
'#': 'ById',
'.': 'sByClassName',
'@': 'sByName',
'=': 'sByTagName'}[s[0]];
return document[c?'getElement'+c:'querySelectorAll'](s.slice(1))
};
// V2 - 160b
window.$ = function(s,c) {
return document[(c = {
'#': 'ById',
'.': 'sByClassName',
'@': 'sByName',
'=': 'sByTagName'}[s[0]])?'getElement'+c:'querySelectorAll'](s.slice(1))
};
// versions with scope
// $('#id',scope); throws error if scope != document
// based on V1 - 167b
window.$ = function(s,p,c) {
c = {
'#': 'ById',
'.': 'sByClassName',
'@': 'sByName',
'=': 'sByTagName'}[s[0]];
return (p||document)[c?'getElement'+c:'querySelectorAll'](s.slice(1))
};
// based on V2 - 165b
window.$=function(s,c){
return (c||document)[
(c={'#':'ById','.':'sByClassName','@':'sByName','=':'sByTagName'}[s[0]])
?'getElement'+c
:'querySelectorAll'
](s.slice(1))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment