[Javascript] AJAX and variable assignment?

Hassan Schroeder hassan at webtuitive.com
Sun Apr 26 09:23:01 CDT 2009


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.



More information about the Javascript mailing list