Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active December 28, 2015 14:59
Show Gist options
  • Save obenjiro/7518438 to your computer and use it in GitHub Desktop.
Save obenjiro/7518438 to your computer and use it in GitHub Desktop.
querySelectorAll polyfill
querySelectorAll = function(index, i, result, magicValue){
magicValue = 1000;
return function(rule){
var head = document.getElementsByTagName('head')[0];
var style = document.createElement('style');
var css = rule + ' { position:absolute; color:red; top: '+ magicValue + 'px;}';
style.setAttribute('type', 'text/css');
if(style.styleSheet && !style.sheet) style.styleSheet.cssText=css;
else style.innerHTML = css;
head.appendChild(style);
document.body.offsetWidth;
for (var child=document.body.firstChild,result=[];
child != null;
child=child.nextSibling) {
try{
var cstyle = window.getComputedStyle ? window.getComputedStyle(child):child.currentStyle;
if (cstyle['top'] == magicValue+'px') {
result.push(child);
}
}catch(e){}
}
head.removeChild(style);
return result;
};
}();
var r = querySelectorAll('div')
console.log(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment