Last active
October 8, 2016 19:42
-
-
Save englishextra/4e13afb8ce184ad28d77f6b5eed71d1f to your computer and use it in GitHub Desktop.
insert text response as fragment into element
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
/*! | |
* 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