473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Global variable not being set

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

Jun 14 '06 #1
6 5081
"ke******@gmail.com" <ke******@gmail.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 14 '06 #2

Lasse Reichstein Nielsen wrote:
"ke******@gmail.com" <ke******@gmail.com> writes:
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 :)

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

Kevin

Jun 14 '06 #3
"ke******@gmail.com" <ke******@gmail.com> writes:
Lasse Reichstein Nielsen wrote:
"ke******@gmail.com" <ke******@gmail.com> writes:
> 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 :)

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
----
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 14 '06 #4

Lasse Reichstein Nielsen wrote:
"ke******@gmail.com" <ke******@gmail.com> writes:
Lasse Reichstein Nielsen wrote:
"ke******@gmail.com" <ke******@gmail.com> writes:

> 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 :)

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


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

Jun 15 '06 #5
"ke******@gmail.com" <ke******@gmail.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 15 '06 #6

Lasse Reichstein Nielsen wrote:

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? :)


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

Jun 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.