473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Access variable defined in a script which is loaded dynamically

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

Mar 7 '06 #1
5 2384


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 7 '06 #2
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

Mar 7 '06 #3


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 7 '06 #4
Martin Honnen wrote:
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

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');
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 7 '06 #5
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter | last post by:
Hello, First of all, sorry for all the code. I don't know how to explain my problem without it. I have a javascript function which can build a webpage dynamically. A striped down version of...
1
by: bscofield | last post by:
I have 4 *.js files that I am referring to with <script src=*.js></script> call the files A, B, C, and D. From file "A" I can view and use variable "Data1" from file "D". From file "B" I...
3
by: John Yopp | last post by:
I seem to be have problems with what appears to be variable scoping. If I define a constant in my main HTML page, I do not seem to be able to reference it within functions in JavaScript include...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
4
by: theo | last post by:
Program flow...load file,then extract the xml text tags from the file,then the number of Xml tags retrieved from the file determines the number of dropdownlist controls instanciated in the...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
3
by: stahl.karl | last post by:
I have a CGI/Perl program that returns a string output. Is it possible to get this into a Javascript variable, where the name of the variable is defined in the Javascript and not in the Perl code?...
2
by: sylver | last post by:
Hi, Following the discussion that can be found here:...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.