Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-23-2009, 04:06 PM   PM User | #1
0x001A4
New Coder

 
Join Date: Feb 2006
Posts: 33
Thanks: 6
Thanked 0 Times in 0 Posts
0x001A4 is an unknown quantity at this point
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.
0x001A4 is offline   Reply With Quote
Old 04-23-2009, 04:46 PM   PM User | #2
0x001A4
New Coder

 
Join Date: Feb 2006
Posts: 33
Thanks: 6
Thanked 0 Times in 0 Posts
0x001A4 is an unknown quantity at this point
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
0x001A4 is offline   Reply With Quote
Old 04-23-2009, 04:51 PM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Old 04-23-2009, 05:18 PM   PM User | #4
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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
shyam is offline   Reply With Quote
Users who have thanked shyam for this post:
oesxyl (04-23-2009)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:27 PM.


Advertisement
Log in to turn off these ads.