selectSingleNode and FireFox

Ajax is cool, for sure.  However sometimes its not so easy to work with, Ajax brings back a problem to web development that had been going by the wayside for a few years.  You see when I started back in early 2000, we had MS IE, Netscape 4, AOL and a slew of other browsers to support.  Writing interactive web programs for that collection of browsers was roughly akin to writing source code for multiple platforms.  Actually that's just what it is, i guess the analogy works too well.  ASP.NET made a lot of this much nicer, ok well in truth I worked on intranet based projects that locked into one browser.  So while the problem may not have gotten any better I couldn't see it ;)   All that changed a couple years ago, working on very public sites again they had to work in FF and IE, granted this is much better than the old browser collection.  So one problem i had yesterday was Firefox kept barking (rudely) about selectSingleNode() is not a function.  It sure is, its part of an xml element!  i said, but it persisted.  After much googling, i was able to write this function to wrap the functionality.  I'm sure there are better ways of doing this, but this is the one I found.  Judging by the amount of googling that i did to find the bits & parts, there are some out there with a similar problem.  I guess what i don't understand is why it takes three lines of code to do this in FF and only one in IE.  Is it that coming from an IE background my point of view is skewed?  Or is this just legitimately more difficult in FF?   I'll bet that most frameworks (atlas/etc..) have this built in and I just didn't see it.  Post a comment and tell me if that's it!

    function SelectSingleNode(xmlDoc, elementPath)
    {
        if(window.ActiveXObject)
        {
            return xmlDoc.selectSingleNode(elementPath);
        }
        else
        {
           var xpe = new XPathEvaluator();
           var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
           var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
           return results.singleNodeValue; 
        }
    }
 
Filed under: , ,

Comments

# i-Catcher development blog » AJAX pains… said:

PingBack from http://icatcherblog.earlsoft.co.uk/2007/03/ajax-pains/

Wednesday, March 21, 2007 8:23 PM
# Giles said:

There's ups and downs to both sides!

It's actually very easy to fix this by WROX - just extend the element object in firefox like so:

if (!window.ActiveXObject) {

Element.prototype.selectNodes = function(sXPath) {

var oEvaluator = new XPathEvaluator();

var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

var aNodes = new Array();

if (oResult != null) {

var oElement = oResult.iterateNext();

while(oElement) {

aNodes.push(oElement);

oElement = oResult.iterateNext();

}

}

return aNodes;

}

Element.prototype.selectSingleNode = function(sXPath) {

var oEvaluator = new XPathEvaluator();

  // FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.

var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

if (oResult != null) {

return oResult.singleNodeValue;

} else {

return null;

}              

}

}

Then voila - you can use it.  Yes it's a bit messy but it means you can call selectSingleNode from both browsers and it just works.

The problem is Microsoft have extended their XML DOM object so much that it's way beyond the original specs of W3C - like so much other stuff they do!

Thankfully they're introducing XMLHttpRequest in IE7 so browsers can do AJAX in a similar manner.

Monday, June 18, 2007 6:26 AM
# bathroom vanities said:

bathroomdesign.greatnow.com/bathroom-vanities bathroom vanities

Tuesday, July 31, 2007 10:37 AM
# bathroom faucets said:

bathroomdesign.greatnow.com/bathroom-faucets bathroom faucets

Thursday, August 02, 2007 3:05 PM
# bathroom sinks said:

bathroomdesign.greatnow.com/bathroom-sinks bathroom sinks

Friday, August 03, 2007 6:21 PM
# bathroom vanity cabinet said:

bathroomdesign.greatnow.com/bathroom-vanity-cabinet bathroom vanity cabinet

Saturday, August 04, 2007 12:41 PM
# bathroom vanity said:

bathroomdesign.greatnow.com/bathroom-vanity bathroom vanity

Saturday, August 04, 2007 7:55 PM
# Jamie Thompson said:

Almost all cross-browser JavaScript issues are easily solved by using one of the many (now mature) frameworks like jQuery, mooTools and prototype. They neatly wrap up and abstract all AJAX/DOM functionality and are constantly updated and patched against new inconsistencies. It might not be what you want to hear but ASP.NET AJAX is a poor cousin in comparison to the open source alternatives. And I say this as an ASP/ASP.NET developer.

Thursday, June 12, 2008 4:07 AM
# ajax_learner() said:

Solved my problem. You are a genius.

By the way, to everyone out there... DO NOT BUY THE BOOK  "BEGINNING AJAX WITH ASP.NET" which read "Full details of this support (for firefox) is beyond the scope of this book". WTF??????

Sunday, June 29, 2008 5:21 PM
# Colm said:

# Jamie Thompson said:

Almost all cross-browser JavaScript issues are easily solved by using one of the many (now mature) frameworks

So how do you solve this problem in jquery then. Or are you just waffling?

Thursday, September 11, 2008 12:23 PM

Leave a Comment

(required) 
(required) 
(optional)
(required)