I was creating a testcase for a bug that is present in every browser and I noticed IE still doesn’t support textContent
as of IE8. I don’t like having to make code that supports both innerText
and textContent
so I implemented the standard myself using IE8’s support for ECMAScript 3.1 accessors. The following is all the code that you need to make textContent
work in IE8.
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", { // It won't work if you just drop in innerText.get // and innerText.set or the whole descriptor. get : function() { return innerText.get.call(this) }, set : function(x) { return innerText.set.call(this, x) } } ); })(); |
Save it as textContent.js and then include the following code to use it.
<!--[if gte IE 8]><script type="text/javascript" src="textContent.js"></script><![endif]--> |
[…] Informationen gibt es unter: http://eligrey.com/blog/post/textcontent-in-ie8 Geschrieben am 30. Juni 2011 um 09:19 Uhr. | Kommentare (0) | Tags in diesem Artikel: […]
This seems to break in Chrome. I had to check that Object.getOwnPropertyDescriptor(Element.prototype, “textContent”) != undefined before looking at .get on it.
Thanks, I updated the code.
So… posted in 2009 and in 2012 I’m still running into it.Thanks, Eli, for this nice piece of code.
And here we are, almost 2014, and I’m just now running into this b/c some places still use IE8…thanks!
Hello from 2015. And yes, we’re still using IE8..!
Hello from 2017, still finding this piece of code useful as we need to support IE8.
hhhh, this is a crazy, this code is eternal, now we are on 2018 🙂
Thanks man
2021, still worth of code! thanks
Thanks form 2021! 😉
This saved my life!!!
Thanks 😀
thanks for the fix.
Great fix, works very well.
Thanks for publishing.
One minor note: the example above has “if gte IE 8” but it should be less than IE8.
With that out of the way, I was using this in testing for awhile but ran into a very odd interaction with jQuery’s load(“page selector”) method where having textContent defined broke the way jQuery processes the loaded HTML document. This is very much an edge case but in case anyone else runs into it…
It’s greater than or equal to because the fix itself relies on features that were only introduced in IE8 and later.
[…] Tämän olisi voinut hoitaa tyylikkäämminkin kuin if-lauseilla, esimerkiksi osoitteessa http://eligrey.com/blog/post/textcontent-in-ie8 esitetyllä […]
[…] Tämän olisi voinut hoitaa tyylikkäämminkin kuin if-lauseilla, esimerkiksi osoitteessa http://eligrey.com/blog/post/textcontent-in-ie8 esitetyllä […]
Not sure this works with html entities.. (or at least breaks whatever jQuery is doing behind the scenes of its append function). See https://github.com/shawnbot/aight/issues/27 .
Turns out it was Text nodes (and other non-Element nodes) that were giving me a problem. The link in my previous comment now has a more complete textContent shim, included as part of aight.
Looks like there’s also a gist that adds the `configurable` (why?) and `enumerable` attributes: https://gist.github.com/eligrey/175652. I’ve also pimped it out in a fork to make it more standards-compliant: https://gist.github.com/usmonster/d15e4475e3983bd35dba .
Still useful in 2014:)
[…] deve adicionar um script de forma condicional para que possa usá-lo no IE8, veja como no post do Eli Grey sobre o […]