Skip to content

Instantly share code, notes, and snippets.

@AlekVolsk
Created May 31, 2019 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlekVolsk/8bc596e7b38cdfcb7a28ba778d838c63 to your computer and use it in GitHub Desktop.
Save AlekVolsk/8bc596e7b38cdfcb7a28ba778d838c63 to your computer and use it in GitHub Desktop.
IE11 polyfill's: matches, closest, forEach
(function () {
// matches
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.matchesSelector ||
Element.prototype.webkitMatchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector;
}
// closest
if (!Element.prototype.closest) {
Element.prototype.closest = function (css) {
var node = this;
while (node) {
if (node.matches(css)) return node;
else node = node.parentElement;
}
return null;
};
}
// forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
if (typeof NodeList.prototype.forEach !== "function") {
NodeList.prototype.forEach = Array.prototype.forEach;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment