Global variable not being set

K

kevinold

Hello everyone,

I'm using the Prototype JS library, but think my problem might be just
be with how I'm initializing and trying to set my javascript variables.

Just a note, I have to pass my JSON in the response body. I'm aware
of the X-JSON header, but am passing too much data to use it. No
problem and I have code that has it working well.

While I'm getting my JSON data back and can do whatever I'd like with
it in "setMakeVars", I'd like to set it to a global var "gljson"
below, but it never gets set.

Everything with the request is fine and there is data in the "json"
var in "setMakeVars", but it's almost like "setMakeVars" doesn't have
access to global vars.

var gljson;
var url = '/my/path/here';
var ajobj = new Ajax.Request(
url,
{
method: 'get',
onComplete: setMakeVars
});

function setMakeVars (transport) {
var json = eval(transport.responseText);
gljson = json;
}

alert(gljson); // is undefined

Has anyone else run into this? Any ideas what I can do to fix it?

Thanks for any help,
Kevin
 
L

Lasse Reichstein Nielsen

While I'm getting my JSON data back and can do whatever I'd like with
it in "setMakeVars", I'd like to set it to a global var "gljson"
below, but it never gets set.

Sure it does. But it doesn't get set immediately.

var ajobj = new Ajax.Request(
url,
{
method: 'get',
onComplete: setMakeVars
});

function setMakeVars (transport) {
var json = eval(transport.responseText);
gljson = json;
}

alert(gljson); // is undefined

Here you make a new asynchroneous HTTP request to a server.
It is set to set a variable when it completes.
Then you try to display the variable immediately, without waiting
for the request to finish, and, unsurprisingly, the variable
isn't set yet.
Has anyone else run into this?

Lots of people. It's a very common error :)

/L
 
K

kevinold

Lasse said:
Sure it does. But it doesn't get set immediately.



Here you make a new asynchroneous HTTP request to a server.
It is set to set a variable when it completes.
Then you try to display the variable immediately, without waiting
for the request to finish, and, unsurprisingly, the variable
isn't set yet.


Lots of people. It's a very common error :)


Thanks for your reply. How would I go about fixing this so that it is
set and stays that way?

Kevin
 
L

Lasse Reichstein Nielsen

How would I go about fixing this so that it is
set and stays that way?

It is set, and it does stay that way. But you have to wait for it to
happen. That means not doing anything to the global variable until
setMakeVars have been called.

In other words, make it so that everything that has to happen on
the page after the request is started, is called from setMakeVars.
E.g.:
----
var gljson
var ajobj = new Ajax.Request(
url,
{
method: 'get',
onComplete: setMakeVars
});

function setMakeVars (transport) {
var json = eval(transport.responseText);
gljson = json;
alert(gljson); // and everything else you want to do.
}

// nothing here, at all. Execution stops after the request is sent
// and is continues by setMakeVars
 
K

kevinold

Lasse said:
It is set, and it does stay that way. But you have to wait for it to
happen. That means not doing anything to the global variable until
setMakeVars have been called.

In other words, make it so that everything that has to happen on
the page after the request is started, is called from setMakeVars.
E.g.:
----
var gljson
var ajobj = new Ajax.Request(
url,
{
method: 'get',
onComplete: setMakeVars
});

function setMakeVars (transport) {
var json = eval(transport.responseText);
gljson = json;
alert(gljson); // and everything else you want to do.
}

// nothing here, at all. Execution stops after the request is sent
// and is continues by setMakeVars

Lasse,

Thanks for the explaination. I understand how the processing works
now. Here's what I'm trying to accomplish. I'm fetching this JSON
data and would like to store it in variables to be used later on down
the code, over and over again. It seems that if the above is true,
then I'll have to make an request each time I want to build a this data
at various points throughout the page.

It's there some way to save the data to a global variable, then use it
at will?

Thanks for all your help with this,
Kevin
 
L

Lasse Reichstein Nielsen

Here's what I'm trying to accomplish. I'm fetching this JSON data
and would like to store it in variables to be used later on down the
code, over and over again.

What do you mean by "later on down the code"?

The execution of scripts in a web browser are typically single
threaded and event driven, i.e., code is being executed in respons to
an event triggered by something (e.g., user action, timer timeout,
Ajax request state change), and the code triggered by an event runs to
completion before the next event is handled.

The only code that is not call by an event handler is the content of
script elements while the page is being loaded. This happens before
most (if not all) events, and you have no control over the timing
of it.
It seems that if the above is true, then I'll have to make an
request each time I want to build a this data at various points
throughout the page.

Are you building the HTML of the page from the data you fetch? In
that case, you will need to do *synchroneous* requests, since you have
to have the data before the browser continues parsing the page.

In that case, you can still store the returned value in a variable
after it is first fetched, and use it again later.
It's there some way to save the data to a global variable, then use it
at will?

Yes, but as long as you don't use it before it has been fetched.

Try looking into synchroneous requests (would the be SJAX? :)
/L
 
K

kevinold

Lasse said:
Are you building the HTML of the page from the data you fetch? In
that case, you will need to do *synchroneous* requests, since you have
to have the data before the browser continues parsing the page.

In that case, you can still store the returned value in a variable
after it is first fetched, and use it again later.


Yes, but as long as you don't use it before it has been fetched.

Try looking into synchroneous requests (would the be SJAX? :)

Lasse,

Yes, I guess this is what I'm trying to accomplish, but I can't get the
syncronous requests to work.

Here's exactly what I'm trying to accomplish:
http://kold.homelinux.com/testselect.html

I've commented the code with what I'm trying to do.

Thanks for any help you can offer,
Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top