Thursday, August 17, 2006

JavaScript library != black box


- Prototype 1.4.0 users - take heed!

Topic: ECMAScript/JavaScript, Ajax, Prototype, XHR

black boxThere's been a lot of hype around the Prototype Library, and others, in the last year or so. This library claims to ease web development, by hiding away complex cross-browser issues, providing a lot of useful functions and wrapping the XHR object for simple Ajax calls. The latest release of the library is 1.4.0, and can be downloaded from their webpage.

Though many people promote that libraries like Prototype will take care of all your Ajax worries, you unfortunately can't treat the library like a black box. You really need to make sure that the library is mature enough to be safe to use. Here are 3 examples of things you need to be aware of with Prototype Library version 1.4.0:

  • It messes with the prototype of the mother of all objects, the Object object, and therefore the for-in control statement.
  • Ajax GET requests will be arbitrarily re-sent in IE7.0
  • Ajax requests will be cached in IE, unless you manually set the correct headers

You can read more about the first point in this blog post from Erik Arvidsson, and Dean Edwards also touches on this in his blog. I'll write about the third point in a separate post, and give and example of how you can avoid XHR caching in IE.

The second point, arbitrary re-sent Ajax requests, is a very serious one when we start thinking about non-idempotent requests. The problem lies in this code snippet from the Prototype Library version 1.4.0:


getTransport: function() {
return Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
}

The Try.these function above enables you to try different function calls until one of them works. So it's plain to see that the XMLHttpRequest object won't be used unless the ActiveXObjects don't exist. Now, think about IE 7.0 that supports all of these, the result would be that you would never get to use the fancy new "native" XMLHttpRequest object. But that's not a problem in itself, the problem lies in the fact that IE7.0 will arbitrarily re-fire your old requests. Brent Ashley discovered this problem back in June. The way to solve this problem is of course to re-arrange the function calls in the Try.these statement. This has not been corrected yet in an official release of the Prototype Library, but it has been fixed in version1.5.0_rc0 , which is only a release candidate.


getTransport: function() {
return Try.these(
function() {return new XMLHttpRequest()},
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')}
) || false;
}

The moral in this post is that you can't necessarily treat a library as a black box, without knowledge of the code inside. I feel it's a drawback that you actually must be qualified to read and understand the code in a library in order to use it, when the libraries themselves claim to spare you from all the details.

I'm looking forward to the official 2.0 release of Prototype...

posted by Roy Ivar Moe at 2:10 PM

0 Comments:

Post a Comment

<< Home