473,587 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AJAX without global variables?

Is there a way to "pass" an XMLHttpRequest object to the callback
function for onreadystatecha nge? Or a way to access it from
onreadystatecha nge? 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.onready statechange = myCallBack;
...
}

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

...
}

Any ideas?

Aug 16 '05 #1
2 4263


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

function myFunc()
{
...
xmlHttp.onready statechange = 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.onready statechange = function () {
if (xmlHttp.readyS tate == 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.onready statechange = function () {
myCallBack(xmlH ttp);
};
and then of course you need a parameter for your global callback
function e.g.
function myCallBack (httpRequest) {
if (httpRequest.re adyState == 4) {
..
}
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 16 '05 #2
na*****@gmail.c om wrote:
Is there a way to "pass" an XMLHttpRequest object to the callback
function for onreadystatecha nge? Or a way to access it from
onreadystatecha nge? 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
19157
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 am passing to it. Everything works fine on IE 6. Here's a couple samples of what I am doing: // using GET url = 'http://www.myserver.com/cgi-bin/funct?key1=value1&key2=value2'
7
2566
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) global variables outside of the files they were defined it, and created some files that just hold global variables definitions (just variables, without any function definition). So, depending on the purpose/category of variables, they're defined...
10
1960
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 hoping someone can enlighten me. var loader = this; this.req.onreadystatechange = function() { loader.onReadyState.call(loader); }
2
1996
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); resObjekt.onreadystatechange = handleResponse(divID); resObjekt.send(null); }
7
3597
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
11855
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);" onclick="show('ajaxrequest.php?action=removefield','div1');show('ajaxrequest.php?action=reloaddiv2','div2')">verwijderen</a> While both seperate actions work they dont when I put them together. Anyone know how to fix this ? My ajax.js with funcition show
10
2245
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 when starting e.g. 5 ajax requests parallele. how to do that correctly? for one case, where I used *different* resp. handlers, I solved the problem by global variables - but now I have a loop of e.g. 20 calls to the *same* r.handler and I don*t...
10
2783
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 exactly 5 minutes then fail. It seems to be occuring on the client side. I can't re-create the issue. It just happens randomly. I think it eventally reaches the server (Apache) because I can see errors in the server log that certain POST/GET...
20
2208
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?. Heres my code: var xmlhttp = false ; if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
0
7924
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
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 we have to send another system
0
1192
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.