Javascript/AJAX cluelessness



  • I've been checking a certain web development/design forum when I came across a curious-looking cry for help on an AJAX-related matter. I knew there are so many people who blindly use AJAX for the most misguided reasons, so my first reaction was to shy away from the thread for it's potential WTF factor, but I figured it won't hurt to take a peek.

    The guy is asking for help on an AJAX request queueing system. Basically, he wants to fire 2 or more requests at the same time, but have them go through one after the other.

    Simple enough, I thought. I advised him to wrap his request-sending code (function sendRequest() { var http = new XMLHttpRequext(); http.send(); etc... }) and put his queueing logic there. I even attached some code to demonstrate.

    Anyway, he said he has his own queueing solution, and he implemented his
    own "AJAX framework" based on it. He then proceeds to paste his code:

    var req = false;
    var queue = false;
    
    function createRequestObject() {
       // -snip-: 23 lines of a function that sets `req` to `new XMLHttpRequest()`
       // or `new ActiveXObject("Microsoft.XMLHTTP")`
    }
    
    function sendData(status, result, str, outputFILE, resultID) {
       count = 1;
       while (count < 2) {
          if (queue == false) {
             count++;
          }
       }
       queue = true;
       createRequestObject();
       req.open('POST', HTTPDomain+outputFILE, true);
       req.onreadystatechange = function() { ParseData(status, result, resultID);};
       req.send(str);
    }
    
    function ParseData(status, result, resultID) {
       /* snip */
       if(req.readyState == 4) { /* When the request finishes */
           queue = false;
           /* snip */
       }
    }
    

    (For the JavaScript/AJAX impaired:
    Notice the potential infinite loop in the sendData function. When I asked him to explain what he was thinking, he replies "yep it'll loop infinitely until queue is true (queue is free)", where "queue = true" is supposed to happen on the XMLHttpRequest event that fires when the request is finished. Which of course, never happens due to the infinite loop.)



  • The XMLHttpRequext is my typo, sorry.

    Also, when I told him about what he had done, he says "it's okay when he tried it," and it will only probably mess up if there's a big chunk of data to send.



  • count = 1;
       while (count < 2) {
          if (queue == false) {
             count++;
          }
       }

    <FONT face=Tahoma>lol :D</FONT>



  • You snipped the best part! I want to see how he got 23 lines out of this:

    req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');



  • @HeroreV said:

    You snipped the best part! I want to see how he got 23 lines out of this:

    req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');

    Heh heh.. the 21 lines in the body goes something like:

      if (window.XMLHttpRequest) {
          try {
             req = new XMLHttpRequest();
          } catch(e) {
             req = false;
          }
       } else if (window.ActiveXObject) {
          try {
             req = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
             try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (e) {
               req = false;
             }
            }
       }
    
       if(!req) {
          alert("Browser does not support HTTP Request.") 
       }


  • @Aescnt said:

    "yep it'll loop infinitely until
    queue is true (queue is free)"



    I get it!!
    "the queue is freed when all it's contents are faxed, which is one of those scenarios not reproducible in a debugger. Isnt that brillant?"

    I have a hunch that his screen name was DeadlyLocks.



  • Nothing wrong with the 23 lines approach, you can do certain things such as loading the very latest version of MSXML supported by your browser. Here's an example....

    <font face="Courier New" size="1">function createXmlHttpRequest()
    {
        if(typeof XMLHttpRequest != "undefined")
        {
            // For Firefox/Safari/Oprah etc.
            return new XMLHttpRequest();
        }
        else if(window.ActiveXObject)
        {
            var aVersions =
            [
                "MSXML2.XmlHttp.5.0",
                "MSXML2.XmlHttp.4.0",
                "MSXML2.XmlHttp.3.0",
                "MSXML2.XmlHttp",
                "Microsoft.XmlHttp"
            ];
           
            // Keep trying each version of MSXML (starting from latest)
            // until you find the one that's supported
            for(var i = 0; i < aVersions.length; i++)
            {
                try
                {
                    var oXhr = new ActiveXObject(aVersions[i]);
                    return oXhr;
                }
                catch(oError) {}
            }
           
            return null;
        }
        else
        {
            return null;
        }
    }</font>



  • @Sunday Ironfoot said:

    Nothing wrong with the 23 lines approach, you can do certain things such as loading the very latest version of MSXML supported by your browser. Here's an example....

    <font face="Courier New" size="1">function createXmlHttpRequest()
    {
        if(typeof XMLHttpRequest != "undefined")
        {
            // For Firefox/Safari/Oprah etc.
            return new XMLHttpRequest();
        }
        else if(window.ActiveXObject)
        {
            var aVersions =
            [
                "MSXML2.XmlHttp.5.0",
                "MSXML2.XmlHttp.4.0",
                "MSXML2.XmlHttp.3.0",
                "MSXML2.XmlHttp",
                "Microsoft.XmlHttp"
            ];
           
            // Keep trying each version of MSXML (starting from latest)
            // until you find the one that's supported
            for(var i = 0; i < aVersions.length; i++)
            {
                try
                {
                    var oXhr = new ActiveXObject(aVersions[i]);
                    return oXhr;
                }
                catch(oError) {}
            }
           
            return null;
        }
        else
        {
            return null;
        }
    }</font>


    Why do you care what version it creates?



  • @null_vector said:

    @Sunday Ironfoot said:
    Nothing wrong with the 23 lines approach, you can do certain things such as loading the very latest version of MSXML supported by your browser. Here's an example....

    <font face="Courier New" size="1">function createXmlHttpRequest()
    {
        if(typeof XMLHttpRequest != "undefined")
        {
            // For Firefox/Safari/Oprah etc.
            return new XMLHttpRequest();
        }
        else if(window.ActiveXObject)
        {
            var aVersions =
            [
                "MSXML2.XmlHttp.5.0",
                "MSXML2.XmlHttp.4.0",
                "MSXML2.XmlHttp.3.0",
                "MSXML2.XmlHttp",
                "Microsoft.XmlHttp"
            ];
           
            // Keep trying each version of MSXML (starting from latest)
            // until you find the one that's supported
            for(var i = 0; i < aVersions.length; i++)
            {
                try
                {
                    var oXhr = new ActiveXObject(aVersions[i]);
                    return oXhr;
                }
                catch(oError) {}
            }
           
            return null;
        }
        else
        {
            return null;
        }
    }</font>


    Why do you care what version it creates?


    You don't necessarily, but you need to handle the situation where different users might have different versions installed.  If you try to create version 4.0 and they have version 3.0 installed, what happens?


Log in to reply