There is a problem brewing for people that wish to use more than one Ajax library in the same page. It ought to be easy, but it's not...

Javascript has a problem - lack of language level namespacing / packaging. So if I write a web page that uses Javascript from 2 authors, both of whom defined a wibble() function, the result will probably be broken, and worse, since the second definition silently overwrites the first, the result will probably be silently broken. Rather than a compile/link error like in most languages, it will just blow up in your face at some stage.

According to Brendan Eich's Javascript talk, this problem is being fixed in Javascript2, but the Sun will be getting dim before all browsers support it enough that we can rely on it.

In the mean time the there is the very real danger of mid air collisions.

DWR has a $ function, copied from Prototype. So do Atlas and many other frameworks. Alex Russell says it is a very frequently requested feature for Dojo.

The $ function is the only function in DWR that isn't in an object based namespace. I'm not keen on it for this reason, but it is very useful: document.getElementById() is just such a mouthful.

But there is a problem:

  • When Prototype changes their definition of $, what is everyone else supposed to do?
  • What happens when Prototype's $ gets over written by a $ from another library without the new features?
  • What happens if another library want to add some feature that Prototype doesn't pick up?
  • What happens when to implementations of $ evolve in incompatible directions?

On one hand $ is a recipe for a mutual exclusion amongst Javascript libraries. It could mean that Prototype can't be used with anything but Script.aculo.us and other systems that go out of their way to evolve with it. Prototype's willingness to play with the "global scope" has caused bugs in DWR in the past that we have needed to provide work-arounds for, so it is a real problem.

On the other hand $ is very useful because you need to do it so often, and $ is something like 14 times less wearing on your keyboard than document.getElementById(). It's one of the sweetest bits of syntactical sugar there is for Javascript. The bad news is that the sugar is laced with another white powder that is both addictive and dangerous.

So here is my proposal:

Ajax library authors except Prototype should get together to define $ to mean basically document.getElementById() and nothing more. Each should do as DWR does and check for the existence of $ before defining it. Prototype on the other hand should not do the same check. Ever. Prototype should ensure that it's definition of $ is always a strict superset of the 'standard' definition.

DWR can be used with any version of Prototype because if Prototype gets declared first, it defines $ in it's superset form, and then as DWR is declared, it notices and does not alter the superset definition. If however DWR gets defined first, it will define $, however Prototype will overwrite the subset definition with it's superset definition.


Comments have been turned off on old posts