小さなAjaxライブラリを作ってみた

ごりごりJavaScriptを使う必要は無いけど、Ajaxは使いたいというときに便利かもしれない。

$Ajax = {
  'createHttpRequest':
    function() {
      if (window.ActiveXObject) {
        try {
          return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            return new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e2) {
            return null;
          }
        }
      } else if(window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else {
        return null;
      }
    },
  '_request':
    function(uri, method, content, async, callback) {
      var req = this.createHttpRequest();
      req.open(method, uri, async);
      req.onreadystatechange = function() {
        if (req.readyState == 4) {
          if (callback != null) {
            callback(req);
          }
        }
      };
      req.send(content);
    },
  'get':
    function(uri, async, callback) {
      this._request(uri, 'get', '', async, callback);
    },
  'post':
    function(uri, content, async, callback) {
      this._request(uri, 'post', content, async, callback);
    }
}

無駄が多くて、やる気なくてごめんなさい。
YUICompressorで圧縮したら532バイトだった。
使うときはこんな感じかな。

$Ajax.get('/', false, function(req){ alert(req.responseText); });