Archives for category: programming

In my first Ajax and REST article, I talked about how the Ajax web programming style and the REST architectural style work well together. While this is true in theory, there are quite a few technical limitations of browsers’ XmlHttpRequest implementations that make it challenging to take advantage of some very basic HTTP capabilities. Case in point, observe this little gem from the MSDN page on the XHR#open method:

Microsoft Internet Explorer caches the results of HTTP GET requests in the Temporary Internet Files (TIF) folder. In most cases, caching improves performance for data that will not change frequently. To guarantee that the results are not cached, use POST.

As Dr. Evil would say, “Riiiiiiiiiight”.

I expect (hope?) that one of these years all of the popular browsers will expose Javascript APIs that provide HTTP client functionality on par with the Apache HTTP Client, but we’ve got a ways to go.

In the meantime, I found this extremely useful and interesting page by Mark Nottingham that tests the correctness of your browsers’ XHR caching. Try it with a couple of different browsers and observe the differences.

I recently discovered that in Javascript v1.2 and later, the output of the logical OR operator is not simply boolean true or false, but rather:

  • the value of first operand, if the operand can be converted to any of the Javascript variants of “true” (anything other than: false, 0, null, undefined, or the empty string) or,
  • the value of the second operand

I’ve found this useful for a number of situations:

lightweight normalization of cross-browser differences

// determine upon which element a Javascript event (e) occurred
var target = /*w3c*/ e.target || /*IE*/ e.srcElement;

provide a default value

var arr = anObject.aPossiblyNullArray || [];
for (var i=0; i<arr.length; i++) {
    // do something (or not)
}

The code above is likely second nature to people coming from a C or Perl background, but when you’ve been using a language like Java for a few years, it’s easy to forget about this sort of idiom.

(idiom source: Javascript: The Definitive Guide, 5th Ed. – page 72)