Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Forked from eligrey/insertAdjacentHTML.js
Last active August 29, 2015 14:21
Show Gist options
  • Save jasdeepkhalsa/7b96ef2562f43d9d0415 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/7b96ef2562f43d9d0415 to your computer and use it in GitHub Desktop.
/*
* 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;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment