Handle OnReadyStateChange with array of XMLHTTP objects

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

    Handle OnReadyStateChange with array of XMLHTTP objects

    I'm trying to make use of XMLHTTP object, but I've come across a
    problem.
    It seems that there is no way to create dynamic array of such XMLHTTP
    objects (to make several requests) and handle them properly.
    I was trying to use such code:

    <script lang="javascrip t">

    function handleOnReadySt ateChange() {
    if (this.readyStat e==4) {
    //some actions
    }
    }

    function createObjArray( ) {
    var XmlHttp = new Array();

    for (var i = 1; i<=10; i++) {
    XmlHttp[i] = new ActiveXObject(" Msxml2.XMLHTTP" );
    XmlHttp[i].open("GET", url, true);
    XmlHttp[i].onreadystatech ange = handleOnReadySt ateChange;
    }
    }

    createObjArray( );
    </script>

    Unfortunately when event handler function is accessed, 'this' isn't
    set to an object which fired event.

    I was doing literally EVERYTHING (or at least I think so) to make it
    work but ... with no success.

    Behavior of this handler is somehow different from what I was used to,
    because I could with no problem do similiar task with Image object.

    <script lang="javascrip t">

    function handleImgOnLoad () {
    document.getEle mentById(this.n ame).src = this.src;
    document.getEle mentById(this.n ame).width = Math.round(this .width/4);
    document.getEle mentById(this.n ame).height =
    Math.round(this .height/4);
    }

    function createImageArra y {
    var images = new Array();

    for(i=1;i<=10;i ++) {
    images[i] = new Image();
    images[i].src = 'some_url';
    images[i].name = i;
    images[i].onload = handleImgOnLoad ;
    }
    }

    createImageArra y();
    </script>

    So my guess is that problem lies in XMLHTTP object which doesn't set
    reference to itself in its event handler.

    Does anyone knows if it is possible to do what I'm trying to do ?
    I would really appreciate any help.
  • Lasse Reichstein Nielsen

    #2
    Re: Handle OnReadyStateCha nge with array of XMLHTTP objects

    k_kubiak@hotmai l.com (Krzysztof Kubiak) writes:
    [color=blue]
    > I'm trying to make use of XMLHTTP object, but I've come across a
    > problem.
    > It seems that there is no way to create dynamic array of such XMLHTTP
    > objects (to make several requests) and handle them properly.
    > I was trying to use such code:
    >
    > <script lang="javascrip t">
    >
    > function handleOnReadySt ateChange() {[/color]

    Here "this" does refer to an instance of ActiveXObject, as can be seen
    by adding:
    alert([this,this instanceof ActiveXObject,t his.readyState]);
    So the "this" is set, it's just the readyState that is broken?
    Or it is not the actual object, but another instance?
    [color=blue]
    > for (var i = 1; i<=10; i++) {
    > XmlHttp[i] = new ActiveXObject(" Msxml2.XMLHTTP" );[/color]

    I noticed that this doesn't create a new object each time, but the
    same object several times. You can see this if you add:
    if (i>1) {alert([i-1,i,XmlHttp[i]==XmlHttp[i-1]]);}

    [color=blue]
    > Behavior of this handler is somehow different from what I was used to,
    > because I could with no problem do similiar task with Image object.[/color]

    Well, it's a host object. They can do pretty much anything they want,
    and still be in compliance with the ECMAScript standard.
    [color=blue]
    > <script lang="javascrip t">[/color]

    type="text/javascript"
    !
    [color=blue]
    > for(i=1;i<=10;i ++) {
    > images[i] = new Image();
    > images[i].src = 'some_url';
    > images[i].name = i;
    > images[i].onload = handleImgOnLoad ;[/color]

    I suggest adding the onload hander before setting the src property.
    That way an already cached image can't be considered loaded *before*
    the handler is set.
    [color=blue]
    > So my guess is that problem lies in XMLHTTP object which doesn't set
    > reference to itself in its event handler.[/color]

    It sets something that is an instance of ActiveXObject. And the same
    object every time (at least according to ==).
    [color=blue]
    > Does anyone knows if it is possible to do what I'm trying to do ?
    > I would really appreciate any help.[/color]

    Can't help you, sorry. Just noticing some more odd behavior.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Handle OnReadyStateCha nge with array of XMLHTTP objects

      Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
      [color=blue]
      > Here "this" does refer to an instance of ActiveXObject, as can be seen
      > by adding:
      > alert([this,this instanceof ActiveXObject,t his.readyState]);[/color]

      Another wonder was that I added:

      var n;
      for(var i=1;i<XmlHttp.l ength;i++) { // I made XmlHttp global for this
      if (this == XmlHttp[i]) {n=i;break;}
      }
      alert([this,n]);

      The wonder is that they all compare equal to XmlHttp[1]. However, if
      I change == to ===, this is not equal to any of the XmlHttp elements.
      [color=blue]
      > I noticed that this doesn't create a new object each time, but the
      > same object several times. You can see this if you add:
      > if (i>1) {alert([i-1,i,XmlHttp[i]==XmlHttp[i-1]]);}[/color]

      But now I can't reproduce that. Probably me seeing spooks.


      Anyway, a solution is to not use the same handleOnReadySt ateChange
      function for all the requests, but create a new one for each request
      that knows what object it belongs to:

      function makeHandleOnRea dyStateChange(X mlHttp) {
      return function() {
      if (XmlHttp.readyS tate == 4) {
      alert(self.resp onsText); // or something
      }
      }
      }

      and then in the loop:

      XmlHttp[i].onreadystatech ange = makeHandleOnRea dyStateChange(X mlHttp[i]);

      It seems to work.
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Martin Honnen

        #4
        Re: Handle OnReadyStateCha nge with array of XMLHTTP objects



        Lasse Reichstein Nielsen wrote:
        [color=blue]
        > k_kubiak@hotmai l.com (Krzysztof Kubiak) writes:
        >
        >[color=green]
        >>I'm trying to make use of XMLHTTP object, but I've come across a
        >>problem.
        >>It seems that there is no way to create dynamic array of such XMLHTTP
        >>objects (to make several requests) and handle them properly.
        >>I was trying to use such code:
        >>
        >><script lang="javascrip t">
        >>
        >>function handleOnReadySt ateChange() {[/color]
        >
        >
        > Here "this" does refer to an instance of ActiveXObject, as can be seen
        > by adding:
        > alert([this,this instanceof ActiveXObject,t his.readyState]);
        > So the "this" is set, it's just the readyState that is broken?
        > Or it is not the actual object, but another instance?[/color]

        It is the window object, try
        window === this
        inside the event handler. And yes
        window instanceof ActiveXObject
        is true, any host object in IE/Win will probably yield true of
        instanceof ActiveXObject.

        The problem is with the onreadystatecha nge event handler not being set
        up to have this point to the object the handler is attached to (and
        firing on).
        With IE this remains the window (global) object but with
        Mozilla/Netscape it is not better as there this in the
        onreadystatecha nge handler is the function itself.
        And the usual way with evt.target respectively window.event.sr cElement
        doesn't work either, Mozilla doesn't even set the evt parameter and IE
        doesn't set the srcElement.
        So indeed the only way for the onreadystatecha nge handler to reliably
        access the object it is firing on is using a closure.



        --

        Martin Honnen


        Comment

        • Krzysztof Kubiak

          #5
          Re: Handle OnReadyStateCha nge with array of XMLHTTP objects

          Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<smj120am. fsf@hotpop.com> ...[color=blue]
          > Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
          >[/color]

          <... snip>
          [color=blue]
          >
          > Anyway, a solution is to not use the same handleOnReadySt ateChange
          > function for all the requests, but create a new one for each request
          > that knows what object it belongs to:
          >
          > function makeHandleOnRea dyStateChange(X mlHttp) {
          > return function() {
          > if (XmlHttp.readyS tate == 4) {
          > alert(self.resp onsText); // or something
          > }
          > }
          > }
          >
          > and then in the loop:
          >
          > XmlHttp[i].onreadystatech ange = makeHandleOnRea dyStateChange(X mlHttp[i]);
          >
          > It seems to work.
          > /L[/color]

          Thanks for pointing me in the right direction. Now I can handle each
          object in the proper manner, although I still have a problem which I
          will probably not solve until next year ;)

          Each of the created event handlers responses independently, but it
          seems that I'm not getting proper responses.
          The reason why I'm using this loop to create XmlHttp objects is to
          find if sequential images exists. I'm iterating through the subsequent
          images (http://myserver.net/img001.jpg,
          http://myserver.net/img002.jpg, etc.) and if I get XmlHttp.statusT ext
          == 'OK' and XmlHttp.getResp onseHeader("Con tent-Type") == 'image/jpeg'
          I'm incrementing counter on the page (inside each of the handlers)
          like this:
          document.getEle mentById('count er').innerHTML =
          parseInt(docume nt.getElementBy Id('counter').i nnerHTML) + 1;

          Now I only get proper Content-type inside first handler, the other
          ones give me Content-Type = 'text/html'.
          I was wondering what 'arrives' in these responses and it was some
          non-default 404 page, which means that file was not found on the
          server (I know it is definitely there).
          So ... I guess I know what I'll be dealing with, after New Year's Eve
          party.

          Once again thanks for your answers

          Comment

          • Martin Honnen

            #6
            Re: Handle OnReadyStateCha nge with array of XMLHTTP objects



            Krzysztof Kubiak wrote:

            [color=blue]
            > The reason why I'm using this loop to create XmlHttp objects is to
            > find if sequential images exists. I'm iterating through the subsequent
            > images (http://myserver.net/img001.jpg,
            > http://myserver.net/img002.jpg, etc.)[/color]

            If you only want to check whether a resource exists on the server then I
            strongly suggest not to send a HTTP GET request but only a HTTP HEAD
            request e.g. use
            XmlHttp[i].open("HEAD", url, true);
            That way the server sends only the HTTP response headers back but not
            the complete image.
            --

            Martin Honnen


            Comment

            • Jim Ley

              #7
              Re: Handle OnReadyStateCha nge with array of XMLHTTP objects

              On Wed, 31 Dec 2003 14:53:33 +0100, Martin Honnen <mahotrash@yaho o.de>
              wrote:
              [color=blue]
              >If you only want to check whether a resource exists on the server then I
              >strongly suggest not to send a HTTP GET request but only a HTTP HEAD
              >request e.g. use
              > XmlHttp[i].open("HEAD", url, true);
              >That way the server sends only the HTTP response headers back but not
              >the complete image.[/color]

              Watch out on many java servers, they often jsut wack out a 500 if you
              try a head on them. (not relevant for static images of course, but if
              you're checking other pages.)

              Jim.
              --
              comp.lang.javas cript FAQ - http://jibbering.com/faq/

              Comment

              Working...