Skip to content

Instantly share code, notes, and snippets.

@keeto
Created December 2, 2011 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeto/1423663 to your computer and use it in GitHub Desktop.
Save keeto/1423663 to your computer and use it in GitHub Desktop.
Element.insertHTML
/*
---
name: Element.injectHTML
description: Wrapper/polyfill around insertAdjacentHTML.
license: MIT-style license
copyright: Copyright (c) 2011 Mark Obcena <keetology.com>
requires: [Element, Function.from]
provides: Element.injectHTML
...
*/
(function(){
var injectHTML = null,
inserters = null;
if ('insertAdjacentHTML' in document.head){
inserters = {
top: 'afterBegin',
bottom: 'beforeEnd',
before: 'beforeBegin',
after: 'afterEnd'
};
injectHTML = function(html, where){
this.insertAdjacentHTML(inserters[where || 'bottom'], html);
return this;
};
} else {
injectHTML = function(html, where){
var temp = new Element('div', {html: html}),
children = temp.childNodes,
fragment = temp.firstChild;
if (!fragment) return this;
if (children.length > 1){
fragment = document.createDocumentFragment();
for (var i = 0, l = children.length; i < l; i++){
fragment.appendChild(children[i]);
}
fragment = {toElement: Function.from(fragment)};
}
return this.grab(fragment, where);
};
}
Element.implement('injectHTML', injectHTML);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment