I have the following JavaScript (I'm using jQuery):
function language(language)
{
var text = new Object();
$.ajax({
type: "GET",
url: "includes/xml/languages/" + language + ".xml",
dataType: "xml",
success: function(xml){
$(xml).find('text').each(function(){
text[$(this).attr('id')] = $(this).text();
});
}
});
return true;
}
I have an XML file which is then being read by the class. The XML file has declarations like this:
<text id="must_be_string">Must be a string.</text>
<text id="must_be_number">Must be a number.</text>
<text id="must_be_integer">Must be an integer.</text>
The XML file is being read correctly, but the problem I'm having is that the text
variables don't seem to be working properly.
From putting some alert stop-points in to try to debug, I've discovered that this is what's happening:
Inside success: function(xml){
, the var text
can be properly accessed. However, the assignment inside that function to assign a new phrase to text doesn't add it correctly. Inside the success:
, I can alert(text['must_be_string'])
and get "Must be a string," but when I leave the Ajax call, it always shows "undefined."
In case I'm not being clear:
var text = new Object();
$.ajax({
type: "GET",
url: "includes/xml/languages/" + language + ".xml",
dataType: "xml",
success: function(xml){
alert(text); // Shows [object Object]
$(xml).find('text').each(function(){
text[$(this).attr('id')] = $(this).text();
});
alert(text['must_be_string']); // Shows "Must be a string."
}
});
alert(text['must_be_string']); // Shows undefined -- this is my problem
I would really, really appreciate any help on this. Please explain because I would really like to understand what's going on.