jQuery’s totally cross-browser capable, right? Well, generally, yes. However, through no fault of jQuery, Safari and Chrome don’t seem to like the z:row namespace that the SharePoint Lists Web Service GetListItems operation returns. I found a nice workaround for z:row from Mike Hacker to improve cross-browser compatibility. I added a check for ItemCount up front so that we don’t try to use the getElementsByTagNameNS function in IE if there have been no rows returned legitimately.
Here’s my function. I call it with the XMLHttpRequest result wherever I’m using GetListItems to get a usable rowset regardless of the browser.
// Find the rows in an XMLHttpRequest. This includes a workaround for Safari and Chrome's
// aversion to the z:row namespace.
function getZRows(rXML) {
var rows;
var itemCount = $(rXML).find("rs\\:data").attr("ItemCount");
if (rXML.getElementsByTagName("z:row").length == 0 && itemCount == undefined) {
rows = rXML.getElementsByTagNameNS('*', 'row');
} else {
rows = rXML.getElementsByTagName("z:row");
}
return rows;
}
I’ve tested this in IE8, Firefox, Safari, and Chrome on my Windows PC. It still doesn’t seem to solve the issue on Safari for Mac, but I’m getting there. This will at least let the functions in v0.5.0 of my jQuery Library for SharePoint Web Services work with all of the main browsers on the Windows side of things.
Marc, you can simplify this expression:
rows = rXML.getElementsByTagName(“z:row”)||rXML.getElementsByTagNameNS(“*”,”row”);
This is what I use on the image rotator for example (see the “build your own” tab):
http://www.pathtosharepoint.com/Pages/ImageRotator.aspx
This is ok in plain JavaScript, but for jQuery there’s actually a better way:
$(rXML).find(“rows”)
That’s it!
The downside is that jQuery doesn’t handle namespaces, so you may need to add a namespace test later in the script. However, it doesn’t seem to be a concern here, as you were using a wildcard.
Thanks, Christophe. As with anything, this library is under continual improvement (I hope!). Suggestions like this are always helpful.
M.
The bad news: the “find” method I suggested above doesn’t seem to work so well (at least with multiple namespaces).
The good news: the workaround below seems to address the issue:
http://kevinwhinnery.com/post/165178165/jquery-xml-parsing-and-xml-namespaces
If you test it, I’d be interested in your conclusions, as I plan to use it myself.
Christophe:
I like Kevin Whinnery’s approach much more than mine because it’s cleaner, smaller code. I just did a quick test, and this:
$(xData.responseXML).find("[nodeName=z:row]")
seems to work fine in IE, Firefox, Safari, and Chrome. I’ll want to do some more testing before I declare victory, but I like it, and thanks!
M.