473,508 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global var javascript

3 New Member
I think the below code should work, but for some reason the setNumbers variable isn't keeping its value after the function. I thought if I declared variables outside of any function they would behave globally, so if I put data into them in a function it would carry over out of the function. Where the alert is at the bottom works, but if its moved one block lower it gives me an undefined. All the var's are delcared in the main part of code. Can someone tell me what I'm doing wrong?


Expand|Select|Wrap|Line Numbers
  1. var setNum = 0;
  2. var imageArray = new Array();
  3. var srcArray = new Array();
  4. var altArray = new Array();
  5. var imageSet = new Array(); /////Declared Here outside of any function
  6. var setNumbers;
  7. var numXml;
  8. var xDocument;
  9. var xmlDoc;
  10. var counter=0;
  11. var cnt = 0;
  12.  
  13.     var URL = "pictures.xml";
  14.     var request = false;
  15.     if (window.XMLHttpRequest) {
  16.         request = new XMLHttpRequest();
  17.     } else if (window.ActiveXObject) {
  18.         try {
  19.             request = new ActiveXObject("Msxml2.XMLHTTP");
  20.         } catch (e) {
  21.             try{
  22.                 request = new ActiveXObject("Microsoft.XMLHTTP");
  23.             } catch (e){}
  24.         }
  25.     }
  26.     request.open("GET", URL, true);
  27.     request.onreadystatechange = function() {
  28.         if (request.readyState == 4 && request.status == 200) { 
  29.             xmlDoc = request.responseXML;
  30.             xDocument=xmlDoc.getElementsByTagName("picture");
  31.             numXml = xDocument.length;
  32.             setNumbers = numXml / 4;
  33.  
  34.             for (i=0;i<numXml;i++){
  35.                 srcArray[i] = xmlDoc.getElementsByTagName("src")[i].firstChild.nodeValue;
  36.                 altArray[i] = xmlDoc.getElementsByTagName("alt")[i].firstChild.nodeValue;
  37.                 var img = "<img src=\'"+srcArray[i]+"\' onClick=\'replaceMain("+i+")\' onmouseover=\"this.style.cursor=\'pointer\'\" alt=\'"+altArray[i]+"\' width=144 height=108 >";
  38.                 imageArray[i] = "<li>"+img+"</li>";
  39.  
  40.                 } 
  41. //calls function fisherYates and puts new data into groups for display
  42. fisherYates(imageArray);                        
  43. while(counter < setNumbers) {
  44.     imageSet[counter]=imageArray[cnt];
  45.     cnt++;
  46.     imageSet[counter]+=imageArray[cnt];
  47.     cnt++;
  48.     imageSet[counter]+=imageArray[cnt];
  49.     cnt++;
  50.     imageSet[counter]+=imageArray[cnt];
  51.     cnt++;
  52.     counter++;
  53. }
  54. alert(imageSet[2]); ///////////works here
  55. }
  56. alert(imageSet[2]); /////////doesn't work says "undefined"
  57. }
  58.     request.send(null);
Feb 29 '08 #1
2 1982
mrhoo
428 Contributor
The first alert fires before the xmlResponse returns a value (or doesn't), the second one after.

Put some code in the loop and look at the headers and text of the xmlHTTPRequest object.
Mar 1 '08 #2
hsriat
1,654 Recognized Expert Top Contributor
I think that the problem is being caused, because AJAX is asynchronous.

So what ever you are doing after sending AJAX request, you may delay the function that needs to manipulate the response text when you are using global variables.
Like this...
Expand|Select|Wrap|Line Numbers
  1. function doAfterAjaxResponds() {
  2.    if (setNumbers=="") {//YOUR GLOBAL VARIABLE CONTAINING RESPONSE
  3.  
  4.       //KEEP CHECKING EVERY 1/10 SEC IF AJAX RESPONDED 
  5.       wait = setTimeout("doAfterAjaxResponds()", 100);
  6.    }
  7.    else {
  8.  
  9.       //DO WHATEVER YOU HAVE TO DO WITH THE RESPONSE TEXT
  10.  
  11.    }
  12. }
Mar 1 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
24159
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(); ...
8
2509
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...
1
1431
by: Robert North | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've just started programming JS, and one question keeps nagging me: I can insert a <script> element anywhere in HTML, and when it's executed...
6
3923
by: Soha | last post by:
I'm a beginner in using javascript and i need a reply so urgent; i have a problem in using javascript with asp the problem is that i want to define a global variable and then change the value of it...
6
1945
by: Bruce | last post by:
Can anyone tell me why this doesn't work? My end goal here is to be able to write Javascript library modules that can be used by both client and server side code. My test here consists of 2...
16
3196
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
0
2427
by: hynek.cihlar | last post by:
A strange behaviour thatI found in ASP.NET 2.0. I am trying to issue a callback request (handled by ICallbackEventHandler and RaiseCallbackEvent) and a regular GET request in the client browser...
3
2754
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
15
2026
by: Bob | last post by:
Is there anyway to access the global object from inside a function other than doing a "var _global = this;" before declaring the function? Thanks
27
1665
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
0
7223
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
7114
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
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
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...
1
7034
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
7488
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
5623
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
1544
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 ...
0
412
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.