最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xml - Javascript object.textContent == undefined in IE 9 only - Stack Overflow

programmeradmin1浏览0评论

I've an issue with IE9 with the following code:

var XMLDocument = data;
                    var erreurs = new Array();

                    var test = data.lastChild.lastChild.childNodes;

                    for(var i=0; i<test.length; i++)
                    {
                        //var testx = test[i].textContent;
                        //alert("Test"+i+" = "+testx);
                        var testx = getText(test[i]);
                        alert(testx);
                        erreurs[i] = testx;
                    }

function getText(el) {
  return el.textContent || el.innerText || el.nodeValue || '';
}

In FF, Opera and Safari, this code works fine. In IE, it gives me:
Test0 = undefined
Test1 = undefined

My XML:

<error>
    <missing>1</missing>
    <missing>2</missing>
    <missing>a</missing>
</error>

I just want to return the values of the nodes "missing".

Thank you very much for your help.

I've an issue with IE9 with the following code:

var XMLDocument = data;
                    var erreurs = new Array();

                    var test = data.lastChild.lastChild.childNodes;

                    for(var i=0; i<test.length; i++)
                    {
                        //var testx = test[i].textContent;
                        //alert("Test"+i+" = "+testx);
                        var testx = getText(test[i]);
                        alert(testx);
                        erreurs[i] = testx;
                    }

function getText(el) {
  return el.textContent || el.innerText || el.nodeValue || '';
}

In FF, Opera and Safari, this code works fine. In IE, it gives me:
Test0 = undefined
Test1 = undefined

My XML:

<error>
    <missing>1</missing>
    <missing>2</missing>
    <missing>a</missing>
</error>

I just want to return the values of the nodes "missing".

Thank you very much for your help.

Share Improve this question asked May 29, 2012 at 18:11 ZorkzydZorkzyd 1,0293 gold badges13 silver badges34 bronze badges 3
  • Your code has return el.textContent || el.innerText || el.nodeValue || '';, so what's the problem? – Esailija Commented May 29, 2012 at 18:15
  • 1 jsfiddle/BdCgL/1 works just fine, I only edited the nodeValue part for ie7 and ie8 but even before that they didn't alert undefined, and ie9 worked fine unedited. – Esailija Commented May 29, 2012 at 18:20
  • After debug with breakpoints, it appears that the interpreter XML of IE returns a different file than FF. I found the missing node elsewhere. Something like: data.lastchild.lastchild.lastchild.lastchild.childnode.text So, I have to search element by node name instead of exploring the tree… – Zorkzyd Commented May 29, 2012 at 18:45
Add a ment  | 

2 Answers 2

Reset to default 3

I found the solution on this site: http://www.chezneg.fr/leblog/chezneg-leblog.php?id_art=125

Seemingly, IE and FF interpreter don't read the XML Document in the same way.
For FF, error tag is located here: data.lastChild.lastChild.childNodes
For IE, error tag is located here: data.lastChild.lastChild.lastChild.lastChild.childNodes
(dixit the debugger)

Therefore, it's a better idea to locate the error tag via the following code: data.getElementsByTagName('error');

Many thanks anyway to Esailija for the help !

I have fixed close problem in IE9 by this code:

function getText(el) {
    return el.textContent || el.text;
}
发布评论

评论列表(0)

  1. 暂无评论