{"id":349,"date":"2009-04-25T17:10:29","date_gmt":"2009-04-25T22:10:29","guid":{"rendered":"http:\/\/eligrey.com\/?p=349"},"modified":"2009-04-25T17:10:29","modified_gmt":"2009-04-25T22:10:29","slug":"textcontent-in-ie8","status":"publish","type":"post","link":"https:\/\/eligrey.com\/blog\/textcontent-in-ie8\/","title":{"rendered":"textContent in IE8"},"content":{"rendered":"<p>I was creating a <a href=\"http:\/\/code.eligrey.com\/testcases\/all\/plaintext.html\">testcase for a bug<\/a> that is present in every browser and I noticed IE <em>still<\/em> doesn&#8217;t support <code>textContent<\/code> as of IE8. I don&#8217;t like having to make code that supports both <code>innerText<\/code> and <code>textContent<\/code> so I implemented the standard myself using IE8&#8217;s support for ECMAScript 3.1 accessors. The following is all the code that you need to make <code>textContent<\/code> work in IE8.<\/p>\n<pre lang=\"javascript\" escaped=\"true\">if (Object.defineProperty &amp;&amp; Object.getOwnPropertyDescriptor &amp;&amp;\n     Object.getOwnPropertyDescriptor(Element.prototype, \"textContent\") &amp;&amp;\n    !Object.getOwnPropertyDescriptor(Element.prototype, \"textContent\").get)\n  (function() {\n    var innerText = Object.getOwnPropertyDescriptor(Element.prototype, \"innerText\");\n    Object.defineProperty(Element.prototype, \"textContent\",\n      { \/\/ It won't work if you just drop in innerText.get\n        \/\/ and innerText.set or the whole descriptor.\n        get : function() {\n          return innerText.get.call(this)\n        },\n        set : function(x) {\n          return innerText.set.call(this, x)\n        }\n      }\n    );\n  })();<\/pre>\n<p>Save it as textContent.js and then include the following code to use it.<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;!--[if gte IE 8]&gt;&lt;script type=\"text\/javascript\" src=\"textContent.js\"&gt;&lt;\/script&gt;&lt;![endif]--&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was creating a testcase for a bug that is present in every browser and I noticed IE still doesn&#8217;t support textContent as of IE8. I don&#8217;t like having to make code that supports both innerText and textContent so I implemented the standard myself using IE8&#8217;s support for ECMAScript 3.1 accessors. The following is all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[15,82,83,90,93,163],"class_list":["post-349","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-browsers","tag-ie","tag-ie8","tag-javascript","tag-javascript-snippets","tag-textcontent"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pfpUD-5D","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/comments?post=349"}],"version-history":[{"count":0,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/349\/revisions"}],"wp:attachment":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/media?parent=349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/categories?post=349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/tags?post=349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}