By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,651 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,651 IT Pros & Developers. It's quick & easy.

Accessing dynamically loaded variables

P: n/a
dnn
How can I access dynamically loaded variables?

I am trying to load an external javascript file dynamically and then
access its variables. The script is loaded by the onload handler.

The code looks like this:
var constants = document.createElement("script");
constants.src = REL_PATH + "extensions_demo/js/constants_" +
MY_LANGUAGE + ".js";
constants.type ="text/javascript";
document.getElementsByTagName("head")[0].appendChild(constants);
alert(window.ROOM_01); // ROOM_01 is a variable out of the external
file

However alert(window.ROOM_01) returns 'undefined'. How can the
variables of the dynamically loaded file be accessed?

Thanx for helping.

Cheers,
Nico

Jun 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
dnn said the following on 6/29/2006 4:45 AM:
How can I access dynamically loaded variables?

I am trying to load an external javascript file dynamically and then
access its variables. The script is loaded by the onload handler.

The code looks like this:
var constants = document.createElement("script");
constants.src = REL_PATH + "extensions_demo/js/constants_" + MY_LANGUAGE + ".js";
constants.type ="text/javascript";
document.getElementsByTagName("head")[0].appendChild(constants);
alert(window.ROOM_01); // ROOM_01 is a variable out of the external file

However alert(window.ROOM_01) returns 'undefined'. How can the
variables of the dynamically loaded file be accessed?


Either by ROOM_01 or window.ROOM_01 or window['ROOM_01']

If it is alerting undefined then something is wrong in your src path.
Or, ROOM_01 is not a global variable.

Try alerting constants.src and make sure it is what you wanted it to be
and that it is a valid path.

Put an alert in the external file to make sure it is getting loaded.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.