Skip to content

Instantly share code, notes, and snippets.

@chrisjlee
Created February 12, 2014 17:39
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save chrisjlee/8960575 to your computer and use it in GitHub Desktop.
Save chrisjlee/8960575 to your computer and use it in GitHub Desktop.
IE document.querySelector() polyfill
if (!document.querySelectorAll) {
document.querySelectorAll = function (selectors) {
var style = document.createElement('style'), elements = [], element;
document.documentElement.firstChild.appendChild(style);
document._qsa = [];
style.styleSheet.cssText = selectors + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
window.scrollBy(0, 0);
style.parentNode.removeChild(style);
while (document._qsa.length) {
element = document._qsa.shift();
element.style.removeAttribute('x-qsa');
elements.push(element);
}
document._qsa = null;
return elements;
};
}
if (!document.querySelector) {
document.querySelector = function (selectors) {
var elements = document.querySelectorAll(selectors);
return (elements.length) ? elements[0] : null;
};
}
@caoshouse
Copy link

What's that window.scrollby doing in there? Just curious because it doesn't seem to contribute anything...

And also scrollBy does not work in IE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment