From jsuser at noopy.org Sun Apr 26 07:27:21 2009 From: jsuser at noopy.org (Javascript List User) Date: Sun, 26 Apr 2009 08:27:21 -0400 Subject: [Javascript] AJAX and variable assignment? Message-ID: Hello, I'm using AJAX (prototype.js) and I'm having problems wrapping my head around how to assign a variable in my code -- because no matter what I try the variable remains undefined. I assume this is because of AJAX being asynchronous but am not quite sure how to assign variables in this context. Ideally I'd like to *not* set asynchronous to false (and I'm not even convinced that asynch being false will work w/Firefox 3.0.x). My end goal is to build a menu from innerHTML of an object that was updated via AJAX. Given what I've observed above, it appears that while I can use AJAX to update the UI with the text/HTML that I'd intended, when I look at object.innerHTML, it shows the *old* HTML not the *new* HTML that I'd put inside of it. For example: Ajax test

I am the start condition.

In the HTML above, do_ajax() updates the UI with the output of my CGI program (the string "I've been touched by AJAX") but $('starter').innerHTML still contains "I am the start condition". The same holds true when I look at the DOM Inspector in Firefox 3.0.x. :-( Does anyone have any suggestions as to how I can do the variable assignment that I propose? -- NP -- Nathan Patwardhan From hassan at webtuitive.com Sun Apr 26 09:23:01 2009 From: hassan at webtuitive.com (Hassan Schroeder) Date: Sun, 26 Apr 2009 07:23:01 -0700 Subject: [Javascript] AJAX and variable assignment? In-Reply-To: References: Message-ID: <49F46E45.7000108@webtuitive.com> Javascript List User wrote: > I'm using AJAX (prototype.js) and I'm having problems wrapping my head > around how to assign a variable in my code -- because no matter what I > try the variable remains undefined. > function do_ajax() { > var url = '/testjs1.cgi'; > var pars = 'ajax=1' > var myAjax = new Ajax.Request( > url, > { > method: 'get', > parameters: pars, > onComplete: function(http) { > assign_it = http.responseText; > $('starter').innerHTML = assign_it; > } > }); > alert("after " + " " + assign_it + " " + $('starter').innerHTML); > } > Does anyone have any suggestions as to how I can do the variable > assignment that I propose? I'm not totally sure what your question is :-) but the reason your alert shows 'null' for 'assign_it' is because the alert executes before the AJAX request returns. Put that statement inside the 'onComplete' function and you'll see the variable has the value assigned to it. HTH, -- Hassan Schroeder ----------------------------- hassan at webtuitive.com webtuitive design === (+1) 408-621-3445 === http://webtuitive.com twitter: @hassan dream. code. From casey.lists at caseyftw.com Sun Apr 26 15:16:17 2009 From: casey.lists at caseyftw.com (Casey) Date: Sun, 26 Apr 2009 13:16:17 -0700 Subject: [Javascript] AJAX and variable assignment? In-Reply-To: References: Message-ID: <1a221fe0904261316v6383ce3bv456b5061fc433dac@mail.gmail.com> On Sun, Apr 26, 2009 at 5:27 AM, Javascript List User wrote: > Hello, > > I'm using AJAX (prototype.js) and I'm having problems wrapping my head > around how to assign a variable in my code -- because no matter what I > try the variable remains undefined. ?I assume this is because of AJAX > being asynchronous but am not quite sure how to assign variables in > this context. ?Ideally I'd like to *not* set asynchronous to false > (and I'm not even convinced that asynch being false will work w/Firefox > 3.0.x). > > My end goal is to build a menu from innerHTML of an object that was > updated via AJAX. ?Given what I've observed above, it appears that > while I can use AJAX to update the UI with the text/HTML that I'd > intended, when I look at object.innerHTML, it shows the *old* HTML not > the *new* HTML that I'd put inside of it. ?For example: > > > ? > ? Ajax test > > ? > ? > ? > > ? > ?

I am the start condition.

> ? > ? > > > In the HTML above, do_ajax() updates the UI with the output of my CGI > program (the string "I've been touched by AJAX") but > $('starter').innerHTML still contains "I am the start condition". ?The > same holds true when I look at the DOM Inspector in Firefox 3.0.x. > :-( > > Does anyone have any suggestions as to how I can do the variable > assignment that I propose? What happens is 1) doAjax() is called. 2) The request is sent. 3) The alert box pops up. 4) doAjax() ends. ... 5) (later) The response is received. 6) The onComplete function is called. 7) DOM is changed, and assign_it is set. So basically, you called the alert too early. Put the alert in your onComplete function. > > -- > NP > > -- > Nathan Patwardhan > _______________________________________________ > Javascript mailing list > Javascript at lists.evolt.org > http://lists.evolt.org/mailman/listinfo/javascript >