Skip to content

Instantly share code, notes, and snippets.

@ivandoroshenko
Created January 22, 2020 16:47
Show Gist options
  • Save ivandoroshenko/0a7f91cfbdd9ae28eac04a5c20218def to your computer and use it in GitHub Desktop.
Save ivandoroshenko/0a7f91cfbdd9ae28eac04a5c20218def to your computer and use it in GitHub Desktop.
js prepend IE polyfill
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('prepend')) {
return;
}
Object.defineProperty(item, 'prepend', {
configurable: true,
enumerable: true,
writable: true,
value: function prepend() {
var argArr = Array.prototype.slice.call(arguments),
docFrag = document.createDocumentFragment();
argArr.forEach(function (argItem) {
var isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.insertBefore(docFrag, this.firstChild);
}
});
});
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment