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 Coder
    Join Date
    Feb 2006
    Posts
    33
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Assign Responsetext to Variable

    I'm trying to assign responseText to a variable. Here is my code:
    Code:
    var news, len, count;
    
    news = "";
    len = 0;
    count = 0;
    
    window.onload = function(){
       var xmlHttp = getXmlHttp();
       var script = "./scripts/getNews.php";
    
       if (xmlHttp != null){
         xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState == 4){
               news = xmlHttp.responseText;
            }
         }
       }
       xmlHttp.open("GET", script, true);
       xmlHttp.send(null);
       alert(news);
       len = news.length;
       alert(news);
       alert(len);
       run();
    }
    The problem is that the first alert will show up empty, the second alert has the value and len is the correct length. If I remove the first alert, len becomes zero and the second alert is blank. I will admit that the second problem is that I don't have a complete grasp of ajax concepts. So the way I'm going about it could be completely backwards.

    Basically I call the run() function when the page is done loading and the run() function will display the value on the page. The reason I don't just display it when readyState becomes 4 is because I need to manipulate it in such a way that I can't do it within that if statement.

  • #2
    New Coder
    Join Date
    Feb 2006
    Posts
    33
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Well it seems to work if I remove the len = news.length; line and establish the length in my run() function. I'm going to leave it like this for now, but if anyone reads this post and notices something that I'm doing wrong (ie. where I'm calling my run() function) please let me know

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by 0x001A4 View Post
    Well it seems to work if I remove the len = news.length; line and establish the length in my run() function. I'm going to leave it like this for now, but if anyone reads this post and notices something that I'm doing wrong (ie. where I'm calling my run() function) please let me know
    I'm not sure but I guess that XMLHttpRequest object don't have a 'length' so the script will stop at that line.

    best regards

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    maybe u should try changing to a synchronous request

    Code:
    xmlHttp.open("GET", script, false);
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    oesxyl (04-23-2009)


  •  

    Posting Permissions

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