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

# 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
# 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
# weblogs.asp.net said:

Selectsinglenode and firefox.. I like it :)

Wednesday, April 20, 2011 1:27 AM
# weblogs.asp.net said:

Selectsinglenode and firefox.. Awesome :)

Friday, May 6, 2011 1:08 PM
# weblogs.asp.net said:

Selectsinglenode and firefox.. I like it :)

Monday, June 20, 2011 3:35 AM
# http://www.thenorthfacejackets.me.uk/ said:

to try and intercept each detected incoming Palestinian rocket (which I'm sure they do at the very least) they are going to need 1000's, maybe.

Wednesday, December 5, 2012 8:22 PM
# Marissa said:

Wonderful post. I learned many interesting things. Thank you)

Thursday, January 31, 2013 5:30 PM
# http://www.dotcomsecretsxx.info said:

Hello, how’s it going? Just shared this post with a colleague, we had a good laugh.

Saturday, March 9, 2013 9:23 PM
# https://delicious.com/archieedwards1964 said:

Exceptional site! Long been scouting about for a long time for something much like this situation. Wherever did you get the detail from? Can you enter a couple more citations for extra viewing.

Friday, March 22, 2013 10:21 PM
# http://ipanks.com/best-hoodia-supplements/ said:

Wow, incredible weblog layout! How long have you ever been running a blog for? you make running a blog look easy. The whole look of your web site is magnificent, as well as the content!

Friday, April 5, 2013 1:40 PM