# .onload var scoping text file

**URL:** <https://forum.kirupa.com/t/onload-var-scoping-text-file/120578>\
**Category:** Uncategorized\
**Created:** [August 25, 2004, 3:28pm UTC](https://forum.kirupa.com/t/onload-var-scoping-text-file/120578 "2004-08-25T15:28:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iansears](https://avatars.discourse-cdn.com/v4/letter/i/85e7bf/32.png) [@iansears](https://forum.kirupa.com/u/iansears)\
**Post date:** [August 25, 2004, 3:28pm UTC](https://forum.kirupa.com/t/onload-var-scoping-text-file/120578/1 "2004-08-25T15:28:38Z")

</div>

I read text file name/values into a swf, but cannot seem to get at those vars **outside of the scope of** the .onload portion of my script. \_global seems to have no effect. What am I missing here?

Example:

```auto
data_file="../localHD_data.txt";

```

The contents of this simple little text file look like this:

```auto
&book_title=Scrambled Eggs&
&ch1_title=Introduction&
&ch2_title=Preparing Eggs&
&ch3_title=Cooking the Eggs&

```

After reading my question, take note that I ALSO have referenced a \_global var in this little script, as it always does display correctly and may shed some light on my problem? here is my script example:

```auto
_global.ISBN="sometext here";

function getTextFileData() {
	textfile_data=new LoadVars();
	textfile_data.onLoad=function() {
		book_title=textfile_data['book_title'];
		chapter1_title=textfile_data['ch1_title'];
		chapter2_title=textfile_data['ch2_title'];
		chapter3_title=textfile_data['ch3_title'];
		isbn_number=ISBN;

//alert_as("book_title :"+book_title+"\r"+"chapter1_title :"+chapter1_title+"\r"+"chapter2_title :"+chapter2_title+"\r"+"chapter3_title :"+chapter3_title+"\r"+"ISBN# :"+isbn_number+"\r");
	}
	textfile_data.load(data_file);
//alert_as("book_title :"+book_title+"\r"+"chapter1_title :"+chapter1_title+"\r"+"chapter2_title :"+chapter2_title+"\r"+"chapter3_title :"+chapter3_title+"\r"+"ISBN# :"+isbn_number+"\r");
}

getTextFileData();

```

When I run this function, I can see the variables if I uncomment the first “alert();” there. Note that the value of isbn\_number also displays, ok?"  
However, if I comment it back out and uncomment the SECOND “alert();” there, only the value of isbn\_number displays in my alert. It is as if the vars/values that are captured and defined WITHIN the .onload portion of the script are “trapped in scope” inside of that .onload function.

How can I reference these vars/values “outside of” my getTextFileData function.

NOTE: I have tried the following but it has no discernable different/improvement:

```auto
function getTextFileData() {
	textfile_data=new LoadVars();
	textfile_data.onLoad=function() {
		_global.book_title=textfile_data['book_title'];
		_global.chapter1_title=textfile_data['ch1_title'];
		_global.chapter2_title=textfile_data['ch2_title'];
		_global.chapter3_title=textfile_data['ch3_title'];
		_global.isbn_number=ISBN;

//alert_as("book_title :"+book_title+"\r"+"chapter1_title :"+chapter1_title+"\r"+"chapter2_title :"+chapter2_title+"\r"+"chapter3_title :"+chapter3_title+"\r"+"ISBN# :"+isbn_number+"\r");
	}
	textfile_data.load(data_file);
//alert_as("book_title :"+book_title+"\r"+"chapter1_title :"+chapter1_title+"\r"+"chapter2_title :"+chapter2_title+"\r"+"chapter3_title :"+chapter3_title+"\r"+"ISBN# :"+isbn_number+"\r");
}

getTextFileData();

```

Frustrated with this scoping issue,  
good grief! Thank you in advance for your time in considering my dilemma here. We are all busy, I know.

Ian

---

<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:** [August 25, 2004, 3:38pm UTC](https://forum.kirupa.com/t/onload-var-scoping-text-file/120578/2 "2004-08-25T15:38:44Z")

</div>

its not a scoping issue, its a timing issue. The onload exists for a reason. Its purpose is to allow you a place to deal with loaded content _when its loaded_. Loading takes time. Not everyone is connected through high-speed fiber instant-access of all the worlds files connection. It takes time to download files - milliseconds, seconds, minutes even. Your script will not have access to any loaded variable until it has been loaded.

When it does load, the script within your onLoad is run - the frame script isn’t run, it has long since passed being run. It was executed way back when when you decided to load external content in the first place. No variables we available then because none existed - they haven’t loaded yet. They do exist within the onLoad, however, because it gets executed after they are in the Flash player.

So, given any frame script where you load variables, you will NOT have access to any variable loaded within that script unless the access is from within the onLoad event handler (or any function called from within it).

---

<div class="post-metadata">

**Author:** ![iansears](https://avatars.discourse-cdn.com/v4/letter/i/85e7bf/32.png) [@iansears](https://forum.kirupa.com/u/iansears)\
**Post date:** [August 25, 2004, 3:58pm UTC](https://forum.kirupa.com/t/onload-var-scoping-text-file/120578/3 "2004-08-25T15:58:00Z")

</div>

senocular,

Thank you for replying to this so quickly, senocular. I understand. I am now considering what code options are available to me. Perhaps a timeline level for i++ i\>1000 then kind of pause loop before reaching for the data? Have you a tried and trusted solution that you would implement in this case?

I have two (2) swfs on the web page. consol.swf that does these loadings and calculations, and header.swf, that displays these text values up in the top of the page. My hope is that when the page is first visited, the consol.swf would load these text file titles, pass them to the header.swf via listeners and they would display as the page loads. A pause in the display as data is gathered is acceptable in my case.

Again thank you for what amounts to a runtime accessible expert resource senocular. Your reply has already narrowed my research significantly and saved me much time. I trust you are correct. \<smile\>

Ian

---

<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:** [August 25, 2004, 4:16pm UTC](https://forum.kirupa.com/t/onload-var-scoping-text-file/120578/4 "2004-08-25T16:16:38Z")

</div>

pause loops wont work. for and while loops stall the player and can cause an error for script execution; its not the way to approach it. If you can’t handle what you need in the onload, then you can poll for values (similarly with what you were talking about with the loop) using an enterFrame event or setInterval. Just keep something running in the background to check every frame (or every time period set by the set interval) to see if those values have loaded, and if so, do what you need to do and kill the enterframe event or clear the interval.

---

<div class="post-metadata">

**Author:** ![iansears](https://avatars.discourse-cdn.com/v4/letter/i/85e7bf/32.png) [@iansears](https://forum.kirupa.com/u/iansears)\
**Post date:** [August 25, 2004, 4:54pm UTC](https://forum.kirupa.com/t/onload-var-scoping-text-file/120578/5 "2004-08-25T16:54:50Z")

</div>

Again, senocular, thank you for your quick reply. This is new territory for me. I will research and post back my solution to close this thread.

Thank you for your generosity and accessibility.  
Ian
