Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

object scope problems

Status
Not open for further replies.

stevecrozz

IS-IT--Management
Jul 4, 2005
20
US
I'm loading an XML file in mozilla here:

if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = fillArrayMoz;
}

fillArrayMoz() contains a number of new object declarations that look like: feeds[1][writePos] = new news()

This creates new news objects inside my function, and it does work this far. It loads an XML file, parses the data into an array and I am able to window.alert(feeds[1][0].propertyA) and get the contents of that object property.

But, outside fillArrayMoz() the new objects cease to exist, even though their container array is global. How do I declare these objects to be global? or how do I return all these objects back through the function loading the XML file?
 
When you say,
their container array is global

I assume you mean
Code:
var feeds = new Array();
...
if (document.implementation && document.implementation.createDocument)
    {
        xmlDoc = document.implementation.createDocument("", "", null);
        xmlDoc.onload = fillArrayMoz;
    }
...


//Do what is needed to load xmlDoc.
...



function fillArrayMoz()  {
  ...
  //Populate arrays.
  ...
  feeds[1] = new Array();
  ...
  feeds[1][0] = new news()
  feeds[1][0].propertyA = something_or_other;
  ...
}

If so, then I too would expect feeds[1][0].propertyA to be available outside of fillArrayMoz().
 
Here's my global array declaration.

var feeds = new Array(dim1length);
for(i = 0; i < dim1length; i++) {
feeds = new Array(dim2length);
for(j = 0; j < dim2length; j++) {
feeds[j] = new Array(dim3length);
for(k = 0; k < dim3length; k++) {
feeds[j][k] = new Array();
}
}
}

I have nested loops in fillArrayMoz() that fill in the array like this: feeds[j][k] = new news(args);

I can post the actual code if you like but it is a little over 100 lines long.
 
You were right, but I was unable to get to my objects because at the time I asked for them (the very next line after calling my importXML function) they didn't actually exist yet. I have to wait for the function to finish before calling the resulting objects. I did that by putting my importXML() function within <head></head> and adding a <body onload="javascript:domystuff()"></body> that way it waits until the scripts have finished their preprocessing before i start doingmystuff() bwahahaha, i rule.
 
Well no. I was just trying to confirm that feeds[] was declared outside of fillArrayMoz().

Anyway, my post probably mixed up the scope of feeds[] and feeds[][]. In your code it looks like all of the arrays of arrays of arrays are declared outside of fillArrayMoz(). That seems right.

I think fillArrayMoz() becomes a member of xmlDoc with this
xmlDoc.onload = fillArrayMoz;

I think xmlDoc has a scope of the if block -
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = fillArrayMoz;
}


One would think anything within that block would have access to any variables declared outside of it.


Falling back to the position that global variables are anathema, possibly use a return statement to pass the populated array out of fillArrayMoz().
Code:
//outermost scope
var feeds = new Array();
...
//All else as you have it.
...
//Plus inside fillArrayMoz()
function fillArrayMoz() {
  var X = new Array();
  ...
  return X;
}

//And eventually
feeds = xmlDoc.onload();


Obviously I have no idea what the problem is. You may wish to start a new thread and get some new thinking.
 
Right! That was gonna be my next suggestion. :).

Glad you found the answer.
 
thanks very much, its working now, and thats 100% better than before
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top