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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX onreadystatechange & readyState

    Hey,

    I have a question re onreadystatechange & readyState. I have this code:

    Code:
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("div1").innerHTML=xmlhttp.responseText;
        }
      }
    what happens if xmlhttp.readyState isn't 4 (completed)?
    i have a piece of code where a couple of calls are made to different forms/divs and sometimes the xmlhttp.readyState isn't 4 and values that were supposed to be send to one form/div are in fact sent to another.
    it's, as I understand it, because xmlhttp.responseText is still being directed to different div.

    how do I make the code 'wait' for xmlhttp.readyState==4?

    cheers,
    Mikey

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by simmons View Post
    Hey,


    what happens if xmlhttp.readyState isn't 4 (completed)?
    i have a piece of code where a couple of calls are made to different forms/divs and sometimes the xmlhttp.readyState isn't 4 ....

    how do I make the code 'wait' for xmlhttp.readyState==4?

    cheers,
    Mikey
    Code:
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
             // put code that must wait for
             //readyState 4 here ...
             document.getElementById("div1").innerHTML=xmlhttp.responseText;
        }
    }

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by simmons View Post
    how do I make the code 'wait' for xmlhttp.readyState==4?
    you don’t need to, the XMLHttpRequest object already waits for that (that’s why you have a readystatechange event).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,609
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Dormilich View Post
    you don’t need to, the XMLHttpRequest object already waits for that (that’s why you have a readystatechange event).
    The ready state change is triggered when ever the state changes, not just when it changes to 4 (although some browsers don't use 1, 2, and 3

    Values it can have are:

    (0) UNINITIALIZED
    (1) LOADING
    (2) LOADED
    (3) INTERACTIVE
    (4) COMPLETED
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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