Created
December 2, 2011 15:37
-
-
Save keeto/1423663 to your computer and use it in GitHub Desktop.
Element.insertHTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
--- | |
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