Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Thessaloniki - Greece
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Why we use this sequence????

    Every time, welll almost, when we create a XMLHttpRequest we write the following, for example:

    1.XMLHttpRequest .onreadystatechange = showPictures;
    2.XMLHttpRequest .open("GET", "file.xml", true);
    3.XMLHttpRequest .send(null);

    The question is why??? I find it more logical if the XMLHttpRequest .onreadystatechange was last. Thank you

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Actually, it makes more sense to set up all your object's parameters before calling a function that procedes to utilize those parameters. For example, if your open() is not asynchronous, then it won't assign your onreadystatechange() to your xmlhttp object until after the send is complete.

    Make sense?
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • Users who have thanked itsallkizza for this post:

    epp1123 (12-04-2008)

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Thessaloniki - Greece
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yes you are right indeed. But now I have another question. When does the response arrive?? It doesn't necessarily readyState = 4. After the open() calling or after the send() calling??? And I believe this doesn't matter if the call is sychronous or asychronous. Thank you again
    Last edited by epp1123; 12-04-2008 at 05:14 PM. Reason: More accurate

  • #4
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    If async is set to true, then you're main Javascript will continue to run (ie the lines following your send()) while waiting for the xmlhttprequest response. Whereas if async is set to false, your JS will stop right after your send() call (allowing you to use that data in subsequent code). Non-asynchronous xmlhttprequests on page load can hardly be considered AJAX, more like client-side includes - but that's just semantics. I only mentioned it in the previous post to demonstrate a point.

    Regardless as to whether your xmlhttprequest is async or not, as soon as your entire response is received, your readyState will switch to 4. Every time the readyState is changed, your onreadystatechange handler will run.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • Users who have thanked itsallkizza for this post:

    epp1123 (12-04-2008)

  • #5
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Thessaloniki - Greece
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yes yes, I agree with you. One more question and I believe I am OK with this. If I write "onreadystatechange " after "send()", is there a possibility the response to be completed before the "onreadystatechange" function is called????

  • #6
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    First, you're actually defining onreadystatechange, not calling it (the xmlhttp object is doing that natively).

    If you define your onreadystatechange after your send():
    - If you're request is not async, you won't have an onreadystatechange at all because it won't define it until the requested data has been received.
    - If you're request is async, then yes, you are running the risk of not defining your onreadystatechange in time to catch your readyState==4. I tested a few browsers and all of them missed the state change to 1, some missed all 4 (the move from 3 to 4 would depend on your target data's filesize).

    Just from a logical point of view, it's a horrible idea to set the onreadystatechange after your send() - it just doesn't make sense. I can't really think of a case where you'd want to do this.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • Users who have thanked itsallkizza for this post:

    epp1123 (12-04-2008)

  • #7
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Thessaloniki - Greece
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I wrote the "onreadystatechange" after send() using a async request and I lost readystate=1, but when I wrote it on top I "catched" it.

    I found out my mistake, I found out why I was thinking wrong. Thank you for help!!!!!!!

  • #8
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Anytime
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •