Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

insertAdjacentHTML polyfill

View insertAdjacentHTML.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
* insertAdjacentHTML.js
* Cross-browser full HTMLElement.insertAdjacentHTML implementation.
*
* 2011-10-10
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
 
if (self.document && !("insertAdjacentHTML" in document.createElementNS("http://www.w3.org/1999/xhtml", "_"))) {
 
HTMLElement.prototype.insertAdjacentHTML = function(position, html) {
"use strict";
 
var
ref = this
, container = ref.ownerDocument.createElementNS("http://www.w3.org/1999/xhtml", "_")
, ref_parent = ref.parentNode
, node, first_child, next_sibling
;
 
container.innerHTML = html;
 
switch (position.toLowerCase()) {
case "beforebegin":
while ((node = container.firstChild)) {
ref_parent.insertBefore(node, ref);
}
break;
case "afterbegin":
first_child = ref.firstChild;
while ((node = container.lastChild)) {
first_child = ref.insertBefore(node, first_child);
}
break;
case "beforeend":
while ((node = container.firstChild)) {
ref.appendChild(node);
}
break;
case "afterend":
next_sibling = ref.nextSibling;
while ((node = container.lastChild)) {
next_sibling = ref_parent.insertBefore(node, next_sibling);
}
break;
}
};
 
}

Thank you. I found this to be handy for old versions of Firefox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.