4 Replies Latest reply on May 31, 2007 4:28 PM by Rothrock

    use of variable outside the function that defines it

    NickTheNameless Level 1
      why can't i use the variable "numOfItems" outside the function in the code i've attached below? i need to have a variable that gets it's value from the number of nodes in an xml document and used later, on the root timeline, outside the onLoad function of the xml, after it's been loaded.



        • 1. Re: use of variable outside the function that defines it
          Greg Dove Level 4
          Don't you mean this?:

          var xml:XML = new XML()
          //See # 1 below
          xml.onLoad = function(){
          var nodes = this.firstChild.childNodes;
          var numOfItems = nodes.length;
          trace(numOfItems); //this trace works and returns the value from the xml document
          //See # 2 below
          };
          xml.load("PSdata.xml");
          trace(numOfItems); //this trace does not work, it shows as undefined
          stop

          If so, then I think its because an object or an XML object doesn't have a reference to its parent...i.e. the timeline in which it was created...and I can think of a couple of options..

          First.
          Option A
          Where #1 comment is place the following line:
          xml.owner = this;
          and where # 2 is place this line:
          this.owner.numOfItems = numOfItems

          Option B
          where #2 is call a function defined on the main timeline with numOfItems as an argument.
          the function can then assign it to a variable in the timelne.
          • 2. Re: use of variable outside the function that defines it
            kglad Adobe Community Professional & MVP
            you're trying to access numOfItems before your xml file has completed loading.

            to remedy, use an onLoad handler.

            and using the var prefix within a function makes the prefixed variable local to the function.
            • 3. Re: use of variable outside the function that defines it
              NickTheNameless Level 1
              ok, so i left out a line in the code in my previous post, but i followed kglad's advice and didn't use var within the onLoad function, it still does not work. again to recap, i'm trying to declare a variable on my root timeline, give the value to that variable via an xml file in an onLoad function, then use that value outside the xml onLoad function.

              • 4. Re: use of variable outside the function that defines it
                Rothrock Level 5
                You are still facing the same problem.

                The trace at the bottom of your code is trying to trace before your xml file has completed loading. Therefore you need to have it wait as well.

                Inside your onLoad

                traceResult();

                And where you have your current trace at the bottom.

                function traceResult(){
                trace(numOfItems);
                }

                That should do it.