473,657 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.create Element("script ");
e.src = url;
e.type="text/javascript";
document.getEle mentsByTagName( "head")[0].appendChild(e) ;
}

Function call : loadScript("tes t.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 2399


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 onreadystatecha nge event handler, Mozilla
a load event listener

function loadScript (url, callback) {
var scriptElement = document.create Element('script ');
scriptElement.t ype = 'text/javascript';
scriptElement.s rc = url;
if (typeof scriptElement.a ddEventListener != 'undefined') {
scriptElement.a ddEventListener (
'load',
function (evt) { callback(); },
false
);
}
else if (typeof scriptElement.a ttachEvent != 'undefined') {
scriptElement.a ttachEvent(
'onreadystatech ange',
function () {
if (scriptElement. readyState == 'complete') {
callback();
}
}
);
}
document.getEle mentsByTagName( 'head')[0].appendChild(sc riptElement);
}

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 onreadystatecha nge event handler, Mozilla
a load event listener

if (typeof scriptElement.a ddEventListener != 'undefined') {
scriptElement.a ddEventListener (
'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.javas cript 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
5978
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 the function looks like this: BEGIN CODE
1
1112
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 CANNOT view nor use variable "Data1" from file "D". What's up with that? Any ideas...?
3
4041
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 files. For instance, I receive the following errors: Error: CONST0 is not defined Source File: test1.js Line: 3
6
4739
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 appreciated. Thanks in advance
4
7447
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 placeholder,the user selects the required tags from the dropdownlists (if 5 Xml tags,then 5 dropdownlists each containing 5 xml tags) and now the btnSave button is selected which extracts the user selection form the dropdownlists.
2
3018
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 or more user controls dynamically by adding to a placeholder defined in page_load. I've included the sample code for how we are accessing one. The user controls are not rocket science - just a few text boxes with public accessor properties. We've...
3
6778
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? For example, it seems that the following should work, but it doesn't appear to. Is this valid code? CGI code --- get_string.pl ------ print "Content-type: text/plain\n\n";
2
1948
by: sylver | last post by:
Hi, Following the discussion that can be found here: http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/fda122b27c008266/909da813c8527282?lnk=gst&q=+dynamically+add+a+script+source&rnum=1#909da813c8527282 I modified mine and was trying to launch an alert message inside the dynamically added script . My sample codes ================================================
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.