Access variable defined in a script which is loaded dynamically

J

jeet_sen

Hi,
My external javascript test.js contains a variable definition var a =
"Hello,world!!";
I dynamically loaded the script test.js using the following fucntion:

function loadScript(url)
{
var e = document.createElement("script");
e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
}

Function call : loadScript("test.js");

Now the problem is, though my script test.js gets loaded but I cannot
access the variable defined in the script.

Can anyone help me out ?

Regards,
Suvajit
 
M

Martin Honnen

jeet_sen wrote:

Now the problem is, though my script test.js gets loaded but I cannot
access the variable defined in the script.

Loading happens asynchronously so don't expect to be able to access the
variables after the loadScript call. Rather you need to wait till the
script has been loaded. Or you need to have the loaded script execute
whatever you want to execute.
 
J

jeet_sen

Hi Martin,
Actually my script contains an object literal , which is actually my
data source.
I cannot perform any action from within the loaded script. After
loading the script dynamically I planned to source in the object
literal data.
My test.js looks like this:
TABLE_ITEMS = {
'name' : 'GS60H',
'level' : 'Library',
'total' : 500,
'failed' : 20,
'waive' : 5
};
After loading test.js I wanted to read in TABLE_ITEMS.

Is there is some way out . Please suggest any alternate plan.

Regards,
Suvajit
 
M

Martin Honnen

jeet_sen wrote:

I cannot perform any action from within the loaded script. After
loading the script dynamically I planned to source in the object
literal data.

You can do that but you have to wait until the script has been loaded
and executed. IE supports the onreadystatechange event handler, Mozilla
a load event listener

function loadScript (url, callback) {
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.src = url;
if (typeof scriptElement.addEventListener != 'undefined') {
scriptElement.addEventListener(
'load',
function (evt) { callback(); },
false
);
}
else if (typeof scriptElement.attachEvent != 'undefined') {
scriptElement.attachEvent(
'onreadystatechange',
function () {
if (scriptElement.readyState == 'complete') {
callback();
}
}
);
}
document.getElementsByTagName('head')[0].appendChild(scriptElement);
}

loadScript(
'file.js',
function () { alert(GOD.name); }
);


Opera 8 however does not support those event handlers/listeners on the
script element, hopefully Opera 9 will do.
 
J

Jonas Raoni

Martin said:
You can do that but you have to wait until the script has been loaded
and executed. IE supports the onreadystatechange event handler, Mozilla
a load event listener

if (typeof scriptElement.addEventListener != 'undefined') {
scriptElement.addEventListener(
'load',
function (evt) { callback(); },
false
);
}

Another solution is to add a kind of notifier in the end of the script.

:
:
:
Loader.Notify('file.js');
 
R

Randy Webb

Jonas Raoni said the following on 3/7/2006 3:15 PM:
Another solution is to add a kind of notifier in the end of the script.

And that idea, to date, is the only reliable way to know a .js file has
loaded. At least to the point of the notifier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top