Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
};
}
@chrisjlee

This comment has been minimized.

Copy link
Owner Author

chrisjlee commented Feb 12, 2014

@danwdart

This comment has been minimized.

Copy link

danwdart commented Jun 17, 2015

Would this be adaptable for use on any Node?

@julienetie

This comment has been minimized.

Copy link

julienetie commented Jul 12, 2015

I think you can add ".call(this));" to the end of the function and replace document with "this.document" and window with "this"

@AnandChowdhary

This comment has been minimized.

Copy link

AnandChowdhary commented Feb 2, 2017

What license is this polyfill available as? MIT?
Would like to use it as part of a commercial project. Thanks in advance!

@bantya

This comment has been minimized.

Copy link

bantya commented Apr 19, 2017

@Peppester

This comment has been minimized.

Copy link

Peppester commented Apr 23, 2017

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

@victornpb

This comment has been minimized.

Copy link

victornpb commented Jul 7, 2017

@Peppester I'm not sure, but it is probably something that makes the css engine refresh and recompute the rules or something like that I would guess...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.