Posts in "Javascript"
Updates to jquery.xmlns.js
I've finally gotten around to updating my XML namespace selector module for jQuery. The new version fixes a few typo-related bugs and brings compatibility with the recently-released jQuery version 1.4.
Grab the source file here: jquery.xmlns.js.
XML Namespace Selectors for jQuery
I hit my first real roadbump with jQuery yesterday, a missing feature that really made me stop and stare in puzzlement: jQuery doesn't support xml-namespace selectors. Since I'm trying to parse WebDAV response bodies, and such documents make extensive use of namespaces, it's quite the issue for me. Or rather, it was quite the issue – read on if you're interested in the details, or just download my solution if you're impatient.
Oh sure, jQuery supports prefix selectors just fine. If your document contains an element <D:response>, you can quite safely query for that element by name as long as you remember to backslash-escape the colon:
$(doc).find("D\\:response")
This works as long as you can guarantee that a single prefix is used for the target namespace, and that it's used uniformly throughout the document. But the XML Namespaces standard, as well as the WebDAV standard, make it very clear that you can't rely on this in general. The node name prefix is a purely syntactic construct, while its actual namespace is a semantic property that can be specified in several different ways.
Fortunately, the CSS Level 3 standard provides a very clear syntax and semantics for namespace-aware queries. After declaring 'D' to be the proper WebDAV namespace URI, the CSS-3 equivalent to the above prefix query would be:
jQuery, first impressions
New projects are always a great opportunity to develop some new skills along the way. With my latest project I've jumped on the chance to tick a box that's spent far too long on my todo-list, and find out what all the fuss is about concerning the "write less, do more" JavaScript library known as jQuery.
I've got a long history with JavaScript. It was the first programming language I ever learned, way back in 1998 – yikes, over ten years ago! – before the AJAX craze and all these modern libraries, toolkits and frameworks. More recently, I've spent a couple of years slinging Dojo code at VPAC. While I enjoyed the Dojo experience, I've always felt slightly uncomfortable with it for reasons that I couldn't quite put my finger on. With only a week or so of jQuery under my belt, I think I can now articulate why.
Dojo, particularly in its early versions but even so today, is a relatively "enterprisey" toolkit – it puts a lot of effort into structuring the development process and ensuring that your project can be broken down in the standard Java-esque style of modules and classes. While this produces some very useful features, such as the excellent module and widget systems, it can also feel quite alien in a language as dynamic as JavaScript. You wind up spending an inordinate amount of time trying to make your JavaScript look less like JavaScript and more like Java, for example by defining classes in "standard" OO style using dojo.declare() rather than using JavaScript's prototype-based object semantics. There are advantages in this approach, but it's clear that you're fighting the language to achieve them.