» Programming » JavaScript » InnerText

Add support for the InnerText JavaScript property to browsers that do not correctly support it, such as some versions of Mozilla Firefox.

Please note: The “Try/Catch” is REQUIRED for this to work properly in some versions of Opera Browser, as Opera properly supports InnerText but attempting to query for its support will throw an error.

1
2
3
4
5
6
7
8
9
10
11
12
try {
  if ( ((typeof HTMLElement) != "undefined")  &&  ((typeof HTMLElement.prototype.__defineGetter__) != "undefined") ) {
    HTMLElement.prototype.__defineGetter__("innerText", function () {
      var r = this.ownerDocument.createRange();
      r.selectNodeContents(this);
      return r.toString();
    }); 
    HTMLElement.prototype.__defineSetter__("innerText", function (str) {
      this.innerHTML = str.replace(/&/g, '&amp;').replace(/>/g, '&gt;').replace(/</g, '&lt;').replace(/\n/g, "<br />\n");
    });
  }
} catch(err) {}