Skip to content

Instantly share code, notes, and snippets.

@englishextra
Last active October 8, 2016 19:42
Show Gist options
  • Save englishextra/4e13afb8ce184ad28d77f6b5eed71d1f to your computer and use it in GitHub Desktop.
Save englishextra/4e13afb8ce184ad28d77f6b5eed71d1f to your computer and use it in GitHub Desktop.
insert text response as fragment into element
/*!
* insert text response as fragment into element
* gist.github.com/englishextra/4e13afb8ce184ad28d77f6b5eed71d1f
* @param {String} t text/response to insert
* @param {Object} c target HTML Element
* @param {Object} [cb] callback function
* insertTextAsFragment(t,c,cb)
*/
var insertTextAsFragment = function (t, c, cb) {
"use strict";
var d = document,
b = d.getElementsByTagName("body")[0] || "",
cN = "cloneNode",
aC = "appendChild",
pN = "parentNode",
iH = "innerHTML",
rC = "replaceChild",
cR = "createRange",
cCF = "createContextualFragment",
cDF = "createDocumentFragment",
f = function () {
return cb && "function" === typeof cb && cb();
};
try {
var n = c[cN](!1);
if (d[cR]) {
var rg = d[cR]();
rg.selectNode(b);
var df = rg[cCF](t);
n[aC](df);
return c[pN] ? c[pN][rC](n, c) : c[iH] = t,
f();
} else {
n[iH] = t;
return c[pN] ? c[pN][rC](d[cDF][aC](n), c) : c[iH] = t,
f();
}
} catch (e) {
console.log(e);
};
return !1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment