PDA

View Full Version : Variable visibilty / usage



tsemann
Nov 23rd, 2009, 06:25 PM
Hello everybody,


I have a little problem concerning variable visiblity and usage within inner functions. This is the javascript code:



function StateSuggestions() {
states = [];
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://localhost:8182/www/search/GEMET.txt", true);
txtFile.onreadystatechange = function(states) {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
lines = txtFile.responseText.split("\n"); // Will separate each line into an array
states.push("testInFunction")
}
}
}
states.push("testOutofIt");
txtFile.send(null);
}


The method "states.push("testoutOfIt")" works (adds an element to the array) but "states.push("testInFunction")" doesnt have an affect on the variable states although I gave the variable "states" as transfer parameter to the function.

Can anybody maybe help me?


Greetings,


TsEMaNN

rnd me
Nov 24th, 2009, 12:01 AM
states is not an array in your onreadystatechange function:


function(states) {


since you re-named a new var states (as an argument),
states is === undefined, and thus has no push method.

tsemann
Nov 24th, 2009, 12:44 AM
I solved the problem using this code. I just had to delete "states" like rnd me said (if anybody has some kind of the same problem):


function StateSuggestions() {
states = [];
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://localhost:8182/www/search/GEMET.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
lines = txtFile.responseText.split("\n"); // Will separate each line into an array
alert(lines[0]);
alert(states.length);
for( i =0;i<lines.length;i++){
states.push(lines[i]);
}
alert(states.length);
}
}
}
txtFile.send(null);

}


Thank you andGreetings,
TsEMaNN