473,473 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

AJAX without global variables?

Is there a way to "pass" an XMLHttpRequest object to the callback
function for onreadystatechange? Or a way to access it from
onreadystatechange? I would like to avoid the use of a global
variable...

Example:

/* How do I get rid of this global variable? */
xmlHttp = new XMLHttpRequest();

function myFunc()
{
...
xmlHttp.onreadystatechange = myCallBack;
...
}

function myCallBack()
{
if (xmlHttp.readyState != 4)
{
return;
}

...
}

Any ideas?

Aug 16 '05 #1
2 4247


na*****@gmail.com wrote:
Is there a way to "pass" an XMLHttpRequest object to the callback
function for onreadystatechange? Or a way to access it from
onreadystatechange? I would like to avoid the use of a global
variable...
xmlHttp = new XMLHttpRequest();

function myFunc()
{
...
xmlHttp.onreadystatechange = myCallBack;


You can use a local variable declared and initialized in the function e.g.
var xmlHttp = new XMLHttpRequest();
and then use a function expression exploiting closures e.g.
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
...
}
};
That would require to use the stuff you have in the callback function
into the body of the function expression.
Or if you want to keep the global callback function then you could use a
function expression with a simple body calling your callback and passing
in the request object e.g.
xmlHttp.onreadystatechange = function () {
myCallBack(xmlHttp);
};
and then of course you need a parameter for your global callback
function e.g.
function myCallBack (httpRequest) {
if (httpRequest.readyState == 4) {
..
}
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 16 '05 #2
na*****@gmail.com wrote:
Is there a way to "pass" an XMLHttpRequest object to the callback
function for onreadystatechange? Or a way to access it from
onreadystatechange? I would like to avoid the use of a global
variable...


This is a great use of closures. You can look at my ajax library at
http://www.ajaxtoolbox.com/ to see how I implemented it without any global
variables sitting around. This also has the benefit of allowing concurrent
requests which do not collide with each other.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 16 '05 #3

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

Similar topics

10
by: Danny | last post by:
Hi all, I am having some odd problems with AJAX on Firefox (1.5). When I use GET as the request method everything works ok, but when I do a POST the remote function doesn't get the parameters I...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
10
by: lkagan2000 | last post by:
I'm reading 'Ajax in Action', (one of the best of the many tech books I've read) and there's an example that I just don't fully understand. If you happen to have the book, it's on page 75. I'm...
2
by: Mark Knochen | last post by:
Hi, i have a little problem with ajax. The follow functions are in my site: function sndReq(ID,divID) { resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
10
by: Daniel Loose | last post by:
hi dear folks, i'm new to ajax. simple request no problem - but it seems when I set the response handler function, I can only pass the function name and not give parameters, so I have a problem...
10
by: mouac01 | last post by:
My site uses lots of AJAX requests to PHP files which then retrieves data from MySQL. About 95% of the time the requests complete without issues. Every once in a while a request will hang for...
20
by: Bryan A | last post by:
Is there a way to add a timeout to this script so that it times out at a certain time. So it would be auto updating every 2seconds and it would timeout like after 100 seconds with a message?. ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.