Permalink
Browse files

firefox polyfill: insertAdjacentHTML(), insertAdjacentText() and inse…

…rtAdjacentElement()
  • Loading branch information...
1 parent 9dc405b commit 3f264d2b043d02ca77dd485e4e86edc1fb81edfe slavic committed Mar 22, 2011
Showing with 37 additions and 0 deletions.
  1. +37 −0 src/ffpolyfill.js
View
@@ -0,0 +1,37 @@
+(function(undefined){
+ // insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
+ // for Netscape 6/Mozilla by Thor Larholm me@jscript.dk
+ if (HTMLElement !== undefined && !HTMLElement.prototype.insertAdjacentElement) {
+ HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode) {
+ switch (where){
+ case 'beforeBegin':
+ this.parentNode.insertBefore(parsedNode,this);
+ break;
+ case 'afterBegin':
+ this.insertBefore(parsedNode,this.firstChild);
+ break;
+ case 'beforeEnd':
+ this.appendChild(parsedNode);
+ break;
+ case 'afterEnd':
+ if (this.nextSibling)
+ this.parentNode.insertBefore(parsedNode,this.nextSibling);
+ else
+ this.parentNode.appendChild(parsedNode);
+ break;
+ }
+ };
+
+ HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr) {
+ var r = this.ownerDocument.createRange();
+ r.setStartBefore(this);
+ var parsedHTML = r.createContextualFragment(htmlStr);
+ this.insertAdjacentElement(where,parsedHTML);
+ };
+
+ HTMLElement.prototype.insertAdjacentText = function(where,txtStr) {
+ var parsedText = document.createTextNode(txtStr);
+ this.insertAdjacentElement(where,parsedText);
+ };
+ }
+})();

0 comments on commit 3f264d2

Please sign in to comment.