In my original post, Using the innerText Property in Firefox, I wrote my example using
document.all to determine when to use
Because the main purpose of that post was to explain to the reader that Firefox does not support the innerText property but the textContent property, I failed to consider other browsers… yes, shame on me! (thanks Paul for bringing this to my attention).
You see, there are browsers that although don’t support document.all, they DO support the innerText property: Safari and Konqueror.
So, it is more efficient to check if the innerText property is supported by the user agent (thanks, Matthias). The way I do it is like this:
var hasInnerText =
(document.getElementsByTagName("body").innerText != undefined) ? true : false;
var elem = document.getElementById('id');
var elem2 = document.getElementById ('id2');
elem.textContent = value;
elem2.textContent = value;
elem.innerText = value;
elem2.innerText = value;
Why am I using
document.getElementsByTagName("body")? Well, because, as you know,
document.getElementsByTagName returns an array of elements with that tag name, and since there’s only ONE body tag, I’m calling the first and only index of that array at once. This is so, as you can see in the example above, if you have more than one element that you need to assign a value to.
I have an example. If you wish, check the source out to see how it works.
- Konqueror 3.5.2
- Firefox 188.8.131.52
- Mac X:
- Firefox 184.108.40.206
- Opera 8.54
- Internet Explorer 6
- Internet Explorer 7.0.5346.5 Beta 2
Remember, this is one way to do it. Cheers!