SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2003
    Location
    somewhere on the Earth
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XmlHttpRequst object's onreaystatechange problems

    consider following code:
    Code:
    <html>
    <body>
    <script>
    
    var rqst	= new XmlHttpRequest;
    rqst.onreadystatechange	= function()
    {
         alert(rqst.readyState);
    };
    
    rqst.open('GET', 'home.htm');
    rqst.send(null);
    
    </script>
    </body>
    </html>
    will alert readyState 1 twice.

    but if call OPEN() BEFORE assigning onreadystatechange handler, it will alert only once.

    can someone tell me why?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1 means loading. So it alerts this while its loading. See the table here to see what the different numbers mean.

    You should check for 4, which means the loading is complete:
    Code:
    rqst.onreadystatechange	= function()
    {
    	if(rqst.readyState == 4) yourFunction();
    }

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...uhm, I think the question has to do with why the ordering of the two lines:

    rqst.onreadystatechange = ...
    rqst.open('GET', 'home.htm');

    is relevant.

  4. #4
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It may have something to do with the fact that the proper syntax is XMLHttpRequest. (Not that I mean to get on your case.)

  5. #5
    SitePoint Zealot
    Join Date
    May 2003
    Location
    somewhere on the Earth
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    ...uhm, I think the question has to do with why the ordering of the two lines:

    rqst.onreadystatechange = ...
    rqst.open('GET', 'home.htm');

    is relevant.

    yeah, that's my question!

    assigning onreadystatechange handler before OPEN() will make readyState change to "1" TWICE!

    say if I want to do something like:
    Code:
    var tip = document.getElementById('tip');  //tip is a DIV tag in the page
    var rqst = new XMLHttpRequest;
    rqst.onreadystatechange = function()
    {
        switch(rqst.readyState)
        {
             case 1:
                 tip.innerHTML += '<p>Just a moment please! </p>';
              break;
    
             case 2:
                 tip.innerHTML += '<p>I'm trying! Be patient!</p>';
              break;
    
             case 3:
                 tip.innerHTML += '<p>Coming soon!</p>';
              break;
    
             case 4:
                 tip.innerHTML += '<p>Okay!</p>';
              break;
    
        }
    };
    rqst.open('GET','somepage.htm');
    rqst.send();
    then I will get two "<p>Just a moment please! </p>" in the DIV tag, which is of course not what I want.

    as the name "onreadystatechange " implies, the event should occur when and only when readyState changes. but when you assign the handler before OPEN(), you get readyState 1 twice, thus the onreadystatechange fires twice, which is NOT right.

    can someone explain why?


Bookmarks

Posting Permissions

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