Supplementary - XMLHTTPRequest Library
A Small JavaScript-Library that Automates working with XMLHTTPRequest
T
he XMLHTTPRequest class underpins the majority of AJAX techniques in web development, but working with it entails certain complexities and considerations. The logic for managing these, however, can be encapsulated in a reusable library, thus simplifying application development, and this page provides a small JavaScript resource that automates the creation and use of XMLHTTPRequest objects.
See the Supplementary Section's Introduction and Overview page for terms of use, a summary and comparison of the libraries available from this site, and other general technical-information.

 var XHRFactory = function ()
    {
    function NullFunc () { }

    var SetNoCaching     = function (XHRObj)           { XHRObj.setRequestHeader ("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); };
    var OverrideMimeType = function (XHRObj, MimeType) { XHRObj.overrideMimeType (MimeType); };
    var ClearBaseHandler = function (XHRObj)           { XHRObj.onreadystatechange = XHRFactory.NullFunc; };

    // --------------------------------------------------------

    function CreateNativeXHR  () { return new XMLHttpRequest ();                      }
    function CreateMSXHR_1    () { return new ActiveXObject  ("Microsoft.XMLHTTP");   }
    function CreateMSXHR_2    () { return new ActiveXObject  ("Msxml2.XMLHTTP");      }

    function ThrowNoXHR       () { throw  new Error ("XMLHTTPRequest not supported"); }

    var CreateXHR_ = function ()
       {
       if (window.XMLHttpRequest)
          {
          SetNoCaching = NullFunc;

          return (CreateXHR_ = CreateNativeXHR) ();

          }

       else if (window.ActiveXObject)
          {
          OverrideMimeType =
          ClearBaseHandler = NullFunc;

          try { var XHRObj = new ActiveXObject ("Msxml2.XMLHTTP"); }
          catch (E)
             {
             try        { XHRObj = new ActiveXObject ("Microsoft.XMLHTTP"); }
             catch (E_) { (CreateXHR_ = ThrowNoXHR) (); }

             CreateXHR_ = CreateMSXHR_1;

             return XHRObj;

             }

          CreateXHR_ = CreateMSXHR_2;

          return XHRObj;

          }

       else { (CreateXHR_ = ThrowNoXHR) (); }

       };


    // --------------------------------------------------------

    return {                                                     // Opening brace is misplaced deliberately to
                                                                 // counter line-breaking defect in JavaScript
       CreateXHR        : function () { return CreateXHR_ (); },
       CreateXHRWrapper : function ()
          {
          var XHRObj = CreateXHR_ ();

          return {                                               // Opening brace is misplaced deliberately to
                                                                 // counter line-breaking defect in JavaScript
             GetXHRObj : function () { return XHRObj; },
             Abort     : function ()
                {
                ClearBaseHandler (XHRObj);
                XHRObj.abort     ();
                },

             DoTxn     : function (URL, Method, ASync, RespHandler, Data, MimeType, ErrHandler)
                {
                var BaseHandler = function ()
                   {
                   try
                      {
                      if (XHRObj.readyState === 4)
                         {
                         BaseHandler = XHRFactory.NullFunc;

                         if (XHRObj.status === 200) { if (RespHandler) { RespHandler (XHRObj); } }
                         else                       { if (ErrHandler)  { ErrHandler  (new Error ("Error code returned from server")); } }

                         }

                      }

                   catch (E) { if (ErrHandler) { ErrHandler (new Error ("Fundamental problem in XHR object")); } }

                   };

                // --------------------------------------------------------

                XHRObj.open (Method, URL, ASync);

                if (MimeType)         { OverrideMimeType (XHRObj, MimeType); }
                if (Method === "GET") { SetNoCaching     (XHRObj);           }
                if (Method === "POST")
                   {
                   XHRObj.setRequestHeader ("Content-Type",  "application/x-www-form-urlencoded");
                   XHRObj.setRequestHeader ("Content-length", Data.length);
                   XHRObj.setRequestHeader ("Connection",    "close");
                   }

                XHRObj.onreadystatechange = BaseHandler;

                XHRObj.send (Data);

                if (ASync === false) { BaseHandler (); }

                }

             };

          }

       };

    } ();
            
Copyright © Dodeca Technologies Ltd. 2007 - 2008