javascript - jquery Cannot read property 'getAttribute' of undefined - Stack Overflow

The javascript I'm using works pletely fine in firefox and ie but has this error when run on chrom

The javascript I'm using works pletely fine in firefox and ie but has this error when run on chrome and safari. I'm not entirely sure why it's failing.

        var response = asyncResult.value;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(response, "text/xml");

        }
        else 
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(response);
        }

        console.log(xmlDoc);
         var changeKey = xmlDoc.getElementsById("t:ItemId")[0].getAttribute("ChangeKey");

The console shows this message but outputs the xmlDoc just fine when I have it set to console.log()


Uncaught TypeError: Cannot read property 'getAttribute' of undefined        r.js 
soapToGetItemDataCallback    r.js
r.onreadystatechange    outlookwebapp-15.js:21 
$h.EwsRequest.$1x_1    outlookwebapp-15.js:21 
(anonymous function)   outlookwebapp-15.js:21

The javascript I'm using works pletely fine in firefox and ie but has this error when run on chrome and safari. I'm not entirely sure why it's failing.

        var response = asyncResult.value;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(response, "text/xml");

        }
        else 
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(response);
        }

        console.log(xmlDoc);
         var changeKey = xmlDoc.getElementsById("t:ItemId")[0].getAttribute("ChangeKey");

The console shows this message but outputs the xmlDoc just fine when I have it set to console.log()


Uncaught TypeError: Cannot read property 'getAttribute' of undefined        r.js 
soapToGetItemDataCallback    r.js
r.onreadystatechange    outlookwebapp-15.js:21 
$h.EwsRequest.$1x_1    outlookwebapp-15.js:21 
(anonymous function)   outlookwebapp-15.js:21
Share Improve this question asked Mar 16, 2015 at 21:17 zoomynnzoomynn 31 gold badge1 silver badge3 bronze badges 6
  • Does your console.log(xmlDoc); output the expected result? – blex Commented Mar 16, 2015 at 21:21
  • yes, in both firefox and chrome – zoomynn Commented Mar 16, 2015 at 21:22
  • getElementsById is an invalid method, so this should fail in all browsers. Perhaps it fails differently in Chrome and Safari than in IE and Firefox. – Rick Hitchcock Commented Mar 16, 2015 at 21:30
  • You're right, I actually copied and pasted the wrong version. At one point, someone suggested I try "byId". I actually originally had getElementsByTagName, which was the source of the original error. Any thoughts ? – zoomynn Commented Mar 16, 2015 at 23:45
  • Does using getElementsByTagName resolve your error? – jasonscript Commented Mar 17, 2015 at 2:10
 |  Show 1 more ment

1 Answer 1

Reset to default 1

The problem is that you are trying to get the element by ID and using [0], I guess you wanna getElementsByTagName because that the result is undefined, the code should be:

var changeKey = xmlDoc.getElementsById("t:ItemId").getAttribute("ChangeKey");

Or if "t:ItemId" is a collection:

 var changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745600653a4635382.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信