Ajax and IE6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vincent

    Ajax and IE6

    Hi all,

    I've seen a lot of ajax examples on the web that used an event handler
    called 'processChange' for the onreadystatecha nge event. Normally,
    this looks like this:

    if(window.XMLHt tpRequest) obj = new XMLHttpRequest( );
    else if(window.Activ eXObject) obj = new
    ActiveXObject(' Microsoft.XMLHT TP');

    obj.onreadystat echange = processChange;
    ....

    function processChange() {
    if(obj.readysta te != 4) return;
    if(obj.status == 200){
    // do something
    }
    }

    Well, this works on IE6 and Firefox, but I would like to use this
    instead of obj inside of the processChange function (this.readystat e
    and this.status). This works fine on firefox, but on IE6 it doesn't.
    Can someone tell me why?

    For the record, I want to use this instead of obj because I want to
    make a series of request with a for loop and since happens all inside
    of the same closure, obj would refer to the last instance of my
    xmlhttprequest.

    Thanks,

    Vincent
  • slebetman

    #2
    Re: Ajax and IE6

    On Jul 22, 3:41 am, Vincent <vincentdeh...@ gmail.comwrote:
    if(window.XMLHt tpRequest) obj = new XMLHttpRequest( );
    else if(window.Activ eXObject) obj = new
    ActiveXObject(' Microsoft.XMLHT TP');
    >
    obj.onreadystat echange = processChange;
    ...
    >
    function processChange() {
       if(obj.readysta te != 4) return;
       if(obj.status == 200){
       // do something
       }
    >
    }
    <snip>
    For the record, I want to use this instead of obj because I want to
    make a series of request with a for loop and since happens all inside
    of the same closure, obj would refer to the last instance of my
    xmlhttprequest.
    >
    Rather than using 'this', which changes meaning depending on the
    context where the code gets executed, there is another trick you can
    use to avoid creating a closure: pass the value of obj instead of
    using obj directly.

    /* function generator allowing us to pass
    * the xmlhttprequest object rather than
    * create a closure:
    */
    function makeProcessChan geHandler (ajaxObj) {
    /* admittedly, it is still a closure
    * only in a different scope
    */
    return function () {
    if(ajaxObj.read ystate != 4) return;
    if(ajaxObj.stat us == 200){
    // do something
    }
    }
    }
    obj.onreadystat echange = makeProcessChan geHandler(obj);

    Comment

    • Henry

      #3
      Re: Ajax and IE6

      On Jul 22, 12:40 pm, slebetman wrote:
      <snip>
      ... , there is another trick you can use to avoid
      creating a closure: pass the value of obj instead of
      using obj directly.
      >
      /* function generator allowing us to pass
      * the xmlhttprequest object rather than
      * create a closure:
      */
      function makeProcessChan geHandler (ajaxObj) {
      /* admittedly, it is still a closure
      * only in a different scope
      */
      return function () {
      if(ajaxObj.read ystate != 4) return;
      if(ajaxObj.stat us == 200){
      // do something
      }
      }}
      >
      obj.onreadystat echange = makeProcessChan geHandler(obj);
      That is creating a closure.

      Comment

      • Jorge

        #4
        Re: Ajax and IE6

        On Jul 22, 1:40 pm, slebetman <slebet...@gmai l.comwrote:
        >
        Rather than using 'this', which changes meaning depending on the
        context where the code gets executed, there is another trick you can
        use to avoid creating a closure: pass the value of obj instead of
        using obj directly.
        >
        /* function generator allowing us to pass
         * the xmlhttprequest object rather than
         * create a closure:
         */
        function makeProcessChan geHandler (ajaxObj) {
          /* admittedly, it is still a closure
           * only in a different scope
           */
          return function () {
            if(ajaxObj.read ystate != 4) return;
            if(ajaxObj.stat us == 200){
            // do something
            }
          }}
        >
        obj.onreadystat echange = makeProcessChan geHandler(obj);
        Yeah !
        Other objects and data could be captured as needed in that same
        closure as well :

        xhr.onreadystat echange = (function (xhr, object, data) {
        return function () {

        //xhr, object, and data have been captured in the closure.

        }
        })(xhr, someOtherObject , i);


        "auto-reposting, closurized, xhr monitor and exerciser"
        http://tinyurl.com/6mm5tm :-)

        --Jorge.

        Comment

        • Jorge

          #5
          Re: Ajax and IE6

          On Jul 22, 5:40 pm, Jorge <jo...@jorgecha morro.comwrote:
          >
          "auto-reposting, closurized, xhr monitor and exerciser"
          http://tinyurl.com/6mm5tm :-)
          >
          BTW, FF3 now seems to handle 6 simultaneous requests...
          Safari, Opera: 4
          FF2: 2
          etc: 2

          --Jorge.

          Comment

          Working...