On Oct 6, 2:37 am, "m.ugues" <m.ug...@gmail.com> wrote:
> HAllo all.
> This piece of code works fine in FIrefox but does not work in IE.
>
> http://pastie.org/642444
>
> The problem is on textContent attribute that in IE is undefined.
The textContent property is part of the W3C DOM 3 Core specification,
so likely to not be implemented in a number of browsers. For modern
browsers, you can use a simple function like:
function getTextContent(el) {
if (typeof el.textContent == 'string')
return el.textContent;
if (typeof el.innerText == 'string')
return el.innerText;
}
To support older browsers too, you can use:
function getTextContent(el) {
if (typeof el.textContent == 'string')
return el.textContent;
if (typeof el.innerText == 'string')
return el.innerText;
return getText2(el);
function getText2(el) {
var x = el.childNodes;
var txt = '';
for (var i=0, len=x.length; i<len; ++i){
if (3 == x[i].nodeType) {
txt += x[i].data;
} else if (1 == x[i].nodeType){
txt += getText2(x[i]);
}
}
return txt.replace(/\s+/g,' ');
}
}
--
Rob