# Can't use variable from outside XML onload function

**URL:** <https://forum.kirupa.com/t/cant-use-variable-from-outside-xml-onload-function/157647>\
**Category:** flash\
**Created:** [August 3, 2005, 2:13pm UTC](https://forum.kirupa.com/t/cant-use-variable-from-outside-xml-onload-function/157647 "2005-08-03T14:13:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sapper](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Sapper](https://forum.kirupa.com/u/Sapper)\
**Post date:** [August 3, 2005, 2:13pm UTC](https://forum.kirupa.com/t/cant-use-variable-from-outside-xml-onload-function/157647/1 "2005-08-03T14:13:52Z")

</div>

I am having trouble passing an array through a function, and then accessing the array from outside the function, here is my code:

```auto

           speechArray = new Array();
 function loadXML(loaded) {
      var arrayVar:Number = 0;
      if (loaded) {
           mainArray = new Array();
           for (i=0; i<this.firstChild.childNodes[2].childNodes.length; i++) {
                mainArray* = new Array();
                for (j=0; j<this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes.length; j++) {
                     mainArray*[j] = new Array();
 for (k=0; k<this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes[j].childNodes.length; k++) {
mainArray*[j][k] = new Speech(this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes[j].childNodes[k].attributes.speaker, this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes[j].childNodes[k].firstChild.nodeValue, i+1, j+1, {ra:100, rb:200, ga:100, gb:20, ba:100, bb:123, aa:100, ab:0}, 0, -100, 100, 200, 200);
                          speechArray.splice(arrayVar, 0, mainArray*[j][k]);
                          arrayVar++;
                     }
                }
           }
      }
      trace(speechArray[0].getspeaker());
 }
 xmlData = new XML();
 xmlData.ignoreWhite = true;
 xmlData.onLoad = loadXML;
 xmlData.load("roj-play.xml");
 

```

I can trace the Array from inside the loadXML function, but not outside it, which is what I want to do. I have read Senocular’s XML specificIssues3 page, and tried the suggestions, but I am not sure which is the best one to use, and how exactly they apply to my specific problem. Any suggestions would be greatly appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![BDore](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@BDore](https://forum.kirupa.com/u/BDore)\
**Post date:** [March 27, 2006, 10:16pm UTC](https://forum.kirupa.com/t/cant-use-variable-from-outside-xml-onload-function/157647/2 "2006-03-27T22:16:21Z")

</div>

Oh, I tried declaring the variable on the \_root, \_global, \_parent…did not work. Undefined all the time.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 28, 2006, 12:52pm UTC](https://forum.kirupa.com/t/cant-use-variable-from-outside-xml-onload-function/157647/3 "2006-03-28T12:52:35Z")

</div>

you’re tracing the variable before its defined. The onLoad event occurs after all the code in that script has been executed. Its purpose for existing is because it takes time to load external information. You cant access a variable defined in an onLoad prior to when the onLoad is fired.

---

<div class="post-metadata">

**Author:** ![BDore](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@BDore](https://forum.kirupa.com/u/BDore)\
**Post date:** [March 28, 2006, 4:30pm UTC](https://forum.kirupa.com/t/cant-use-variable-from-outside-xml-onload-function/157647/4 "2006-03-28T16:30:48Z")

</div>

Thanks for the explanation.

I found a way around this though.

On my first frame of my main timeline:

```auto

_quality="BEST";
xmlCanais = new XML();
xmlCanais.ignoreWhite = true;
xmlCanais.load("conteudo.xml");
stop();
this.onEnterFrame = function(){
	if (_root._framesloaded == _root._totalframes && xmlCanais.loaded == true){
		delete onEnterFrame;
		play();		
	}
}

```

So it only starts to play when everything’s loaded.

Later on this is what I got inside my movieclip:

```auto

var dragBottom:Number;

function carregaXML () {
	if (_root.xmlCanais.status == 0) {
		main.descricao.text = _root.xmlCanais.firstChild.childNodes[2].firstChild.childNodes[1] add _root.xmlCanais.firstChild.childNodes[2].firstChild.childNodes[2];
		
		main.mcCanaisLinks._y = 		
		dragBottom = main.descricao.textHeight + 300;		
		
		aLinks = _root.xmlCanais.firstChild.lastChild.firstChild.childNodes;
		nLinks = aLinks.length;
		
		main.mcCanaisLinks.mcLinks.mcItem._visible = false;
		criaBotoes();
		stop();
		} else {
			gotoAndPlay(1);
			};
	};

this.onEnterFrame = carregaXML();

main.setMask(mask_mc);
fscommand("allowscale", "false");
bar.useHandCursor = dragger.useHandCursor=false;
space = 30;
friction = 0.9;
speed = 4;
y = dragger._y;
top = main._y;
bottom = 0 - dragBottom;
trace(dragBottom);

```

The \*\*dragBottom \*\*variable is what I wanted to access. That last trace now returns me some negative value, exactly what I wanted. Works perfectly.

I could have used the loaded property to check if my XML is loaded ok, or even better, used no if at all since I already know my XML is loaded.

:azn:
