Skip to content

Instantly share code, notes, and snippets.

@joshrhoades
Created February 7, 2014 23:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshrhoades/8874249 to your computer and use it in GitHub Desktop.
Save joshrhoades/8874249 to your computer and use it in GitHub Desktop.
IE8 polyfill/shim for supporting textContent (vs innerText) so that textContent is universally accessible. Can be embedded as a standalone script in a `[if lte IE 8]` embed.
if (Object.defineProperty && Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(Element.prototype, "textContent") && !Object.getOwnPropertyDescriptor(Element.prototype, "textContent").get) {
(function() {
var innerText = Object.getOwnPropertyDescriptor(Element.prototype, "innerText");
Object.defineProperty(Element.prototype, "textContent",
{
get: function() {
return innerText.get.call(this);
},
set: function(s) {
return innerText.set.call(this, s);
}
}
);
})();
}
@davidmccabe
Copy link

Note that innerText will not return the text of an element that is hidden, which is the main behavioral problem with innerText.

@usmonster
Copy link

For those interested, I put together a more complete shim, which addresses some of the more subtle problems of the "innerText substitution" solution. It's included in aight. Enjoy!

@enricosoft
Copy link

Hi,
I've some problems using this polyfill... it seems that it causes problem with jQuery append\prepend functions.... is it possible?

@manngo
Copy link

manngo commented Oct 17, 2016

@enricosoft: Not surprising. I haven’t looked into it but it’s possible that jQuery modifies its behaviour depending on whether textContent exists. That shouldn’t matter, but sometimes people use one test to imply another (if it has textContent, then maybe it also has …). Alternatively, maybe this polyfill is missing something subtle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment