May 30th 2007 09:37 pm

The One Line Cross-Browser Ajax Request Object

The simple task of getting a working XMLHTTPRequest object can quickly turn into a mess of if/then browser checking. With the help of one of my favorite operators, the ternary operator, this becomes a simple one-liner:

  reqObj = window.XMLHttpRequest
      ? new XMLHttpRequest() :
        new ActiveXObject("Microsoft.XMLHTTP");

There you go, it’s that easy? Now, of course, this is an extremely stripped down browser check, with no error checking, so depending on your need, this may not be the best way to go.

No Comments yet »

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.

« Easily Syncing Remote Files in Komodo With Unison | Learn Python in 10 Minutes »