Sunday, February 13, 2011

Baffled by an XPath issue on IE

David Flanagan's excellent book on JavaScript has an example that shows how to perform XPath queries in IE. On page 518 of fifth edition, you can see the following code snippet taken from example 21-10:

// In IE, the context must be an Element not a document,
// so if the context is a document, use the documentElement instead
if (context == doc) context = doc.documentElement;
return context.selectNodes(this.xpathText);

I found out the hard way that this code is absolutely necessary. It appears (although I cant understand why) that on IE XMLHttpRequest seems to to randomly return a reference to either the document corresponding to the received XML or the documentElement. Whats happening here?

  • I believe you answered your own question, every so often IE returns a reference to the document, or to the document element, the if statement is simply a quick conditional to fix it if needed.

    If you are asking why IE does it, I don't think anyone here is going to be able to give that answer....

    Rangachari Anand : Thanks! Good to know I am not going mad :-) I somehow believed (like Einstein) that God (and MS) does not play dice with the universe!
    Mitchel Sellers : But we all know they do! Also, after looking, it seems that the issue you listed is a bit more common on IE 6 than other versions..

0 comments:

Post a Comment