Skip to content Skip to sidebar Skip to footer

Uncaught Typeerror: Cannot Call Method 'haschildnodes' Of Undefined

I am trying to read a XML using ajax request in jquery. Below is the code,it's working fine in IE but when I run this on Chrome i am facing this error Uncaught TypeError: Cannot c

Solution 1:

You're doing this:

z=childs[i].childNodes;

then, in the first line of adChilds(z,childs,oNode);, you're doing this:

if(a[i].hasChildNodes()){

But, i isn't an index into the children of a. It's an index into the parent's of a's children. Thus, if the parent of a doesn't have the same number of children as a has children, you will go out of index.

I don't know exactly what you're trying to accomplish in adChilds() so I'm not sure what fix to suggest, but I presume you that if you want to deal with the children of a, you should get the number of children of a and make sure you only access the number that actually exist


Some coding suggestions:

  1. I'd strongly suggest you use real variable names. Names like a, b, c and z for intermediate variables are cryptic and make your code hard to read.

  2. All local variables should be preceded with var at first definition (or defined at the top of the function), otherwise they become global variables which is asking for trouble, especially with asynchronous callback functions.

  3. When you see errors that aren't obvious to you upon first inspection of your code, then set a breakpoint in your favorite debugger and step through the code and examine the state of the variables to see exactly why you're getting the error. If you don't know how to use a debugger, learn. They are built into most browsers, easy and absolutely essential to efficient debugging.

Post a Comment for "Uncaught Typeerror: Cannot Call Method 'haschildnodes' Of Undefined"