global var javascript

K

Kalyke

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?


Code:
var setNum = 0;
var imageArray = new Array();
var srcArray = new Array();
var altArray = new Array();
var imageSet = new Array(); /////Declared Here outside of any function
var setNumbers;
var numXml;
var xDocument;
var xmlDoc;
var counter=0;
var cnt = 0;

var URL = "pictures.xml";
var request = false;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){}
}
}
request.open("GET", URL, true);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
xmlDoc = request.responseXML;
xDocument=xmlDoc.getElementsByTagName("picture");
numXml = xDocument.length;
setNumbers = numXml / 4;

for (i=0;i<numXml;i++){
srcArray[i] = xmlDoc.getElementsByTagName("src")
[i].firstChild.nodeValue;
altArray[i] = xmlDoc.getElementsByTagName("alt")
[i].firstChild.nodeValue;
var img = "<img src=\'"+srcArray[i]+"\' onClick=\'replaceMain("+i
+")\' onmouseover=\"this.style.cursor=\'pointer\'\" alt=\'"+altArray[i]
+"\' width=144 height=108 >";
imageArray[i] = "<li>"+img+"</li>";

}
//calls function fisherYates and puts new data into groups for display
fisherYates(imageArray);
while(counter < setNumbers) {
imageSet[counter]=imageArray[cnt];
cnt++;
imageSet[counter]+=imageArray[cnt];
cnt++;
imageSet[counter]+=imageArray[cnt];
cnt++;
imageSet[counter]+=imageArray[cnt];
cnt++;
counter++;
}
alert(imageSet[2]); ///////////works here
}
alert(imageSet[2]); /////////doesn't work says "undefined"
}
request.send(null);
 
J

Jeremy J Starcher

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?

There isn't enough information to answer that question without creating
my own "pictures.xml" file.

Post a link to a web site and I'll look again.

(In the future, please change the indent to TWO spaces (NOT TABS) when
posting on usenet.)
 
T

Thomas 'PointedEars' Lahn

I think the below code should work, but for some reason the setNumbers
variable isn't keeping its value after the function.

That only seems to be so.
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.

That is correct. However, the method containing the assignment would have
to be called first.
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?

Most likely you are ignoring that the request is asynchronously handled,
which is why the event listener would not have been called or the condition
in it would not apply yet after

request.send(null);
Code:
[/QUOTE]

Despite what Google Groups wants you to believe, this is not a Web forum,
but a public Usenet newsgroup.  It is plain text only, so phpBB-style `code'
tags have no effect and are unnecessary.  Indent your code properly instead,
with two or four spaces instead of tabs, and post it so that automatic word
wrap at 80 characters does not break it.  This will not only help people
doing a helpful analysis without greater effort, but it will also help you
as you can see more clearly what and where you are doing wrong.


PointedEars
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top