473,785 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(Reliably) calling a function from a dynamically loaded script

Hi,

Is there a way to know when a function is available for me to call it
from a dynamically loaded a javascript?

I use this code to load the include.js file and then I call
testIncludeFn() from it:

<code>

var html_doc = document.getEle mentsByTagName( 'head').item(0) ;

var file = document.create Element('script ');
file.setAttribu te('language',' javascript');
file.setAttribu te('type','text/javascript');
file.setAttribu te('src','/include.js');
html_doc.append Child(file);

// here I would like to be able to wait for the script to become
available

testIncludeFn() ;

</code>

It works in IE and sometimes I get it to work in FF 1.5, but I would
like to have a way of reliably calling testIncludeFn() .
Can I pool somehow the DOM to see if the load completed. How?

Is there a better approach to it?

Regards,

Sebastian

Jun 14 '06 #1
5 2222


sf****@gmail.co m wrote:
Is there a way to know when a function is available for me to call it
from a dynamically loaded a javascript? Can I pool somehow the DOM to see if the load completed. How?


Mozilla (and Opera 9 beta) supports e.g.
var script = document.create Element('script ');
script.addEvent Listener(
'load',
function (evt) {
// handle load here e.g. call whatever function you want to call
functionName();
},
false
)
script.type = 'text/javascript';
script.src = 'file.js';
// now append script element into the document tree so that the script
// file is loaded and executed
document.getEle mentsByTagName( 'head')[0].appendChild(sc ript);

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 14 '06 #2
Martin Honnen wrote:
var script = document.create Element('script ');
script.addEvent Listener(
'load',
function (evt) {
// handle load here e.g. call whatever function you want to call
functionName();
},
false
)


Thanks Martin, it works great! and it's really simple this way.

IE6 seems to be a different story though.
I tried both the equivalent with attachEvent('on load' ... and
YAHOO.util.Even t.addListener( script,'load',c allback);
I'm not getting any error but IE doesn't seem to react to load on the
script tag.

Any idea how to get around it?

Regards,
Sebastian

Jun 14 '06 #3
sfe...@gmail.co m wrote:
IE6 seems to be a different story though.
I tried both the equivalent with attachEvent('on load' ... and
YAHOO.util.Even t.addListener( script,'load',c allback);
I'm not getting any error but IE doesn't seem to react to load on the
script tag.


I would use.
var script = document.create Element('script ');
script.onload = function(e) {
functionName();
};
script.src = '......';

Still works fine on Mozilla.
Not sure on Internet Explorer, but in JS reference, it said it was
available since IE4.

Jun 14 '06 #4

the DtTvB wrote:
sfe...@gmail.co m wrote:
IE6 seems to be a different story though.
I tried both the equivalent with attachEvent('on load' ... and
YAHOO.util.Even t.addListener( script,'load',c allback);
I'm not getting any error but IE doesn't seem to react to load on the
script tag.


I would use.
var script = document.create Element('script ');
script.onload = function(e) {
functionName();
};
script.src = '......';

Still works fine on Mozilla.
Not sure on Internet Explorer, but in JS reference, it said it was
available since IE4.


I've tried that before and it didn't work for me. One thing that did
work though was using "onreadystatech ange" instead.

Thanks,
Sebastian

Jun 14 '06 #5
the DtTvB wrote:
sfe...@gmail.co m wrote:
IE6 seems to be a different story though.
I tried both the equivalent with attachEvent('on load' ... and
YAHOO.util.Even t.addListener( script,'load',c allback);
I'm not getting any error but IE doesn't seem to react to load on the
script tag.


I would use.
var script = document.create Element('script ');
script.onload = function(e) {
functionName();
};
script.src = '......';

Still works fine on Mozilla.
Not sure on Internet Explorer, but in JS reference, it said it was
available since IE4.


It does work if you declare both:

script.onload = script.onreadys tatechange = function(){
alert("onLoad") ; };

Regards,
Sebastian

Jun 14 '06 #6

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

Similar topics

7
1647
by: JCO | last post by:
I have several JavaScripts in a web page. Each script is in an individual file with extension .js. I want to know if it is possible to put all functions in one single page. If so, how do I call the specific script that I want to run. This is how I call an individual script: <SCRIPT LANGUAGE="JavaScript" src="valForm.js"></SCRIPT> Where valForm is a script that looks like this: function valForm()
5
2781
by: danny.myint | last post by:
I was under the assumption that javascript loads all the <head></head> elements before processing the <body> tag in Mozilla/Netscape/Firefox. It doesn't seem like it, with my problem. I have navigation that has onMouseOver and onMouseOut triggers, that use function calls in my external .js file. When I mouseOver a button in my navigation quickly enough to catch it before the rest of the body loads, it returns a function "not defined"...
9
2900
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with no luck though), and searched through the IRT.ORG Faqs (www.irt.org/script/script.htm). The Javascript is designed to open an popup window and then inside that window call another script which will resize that window. There may be another...
4
7010
by: Jonathan Roewen | last post by:
Hi I've got loading assemblies dynamically done (wasn't too difficult). Now I want to lookup a static function in the loaded assembly, and if found, return it somehow, and call it from my app. So far, my efforts have failed miserably My function takes custom objects as parameters, along the lines of: string MyFunction(XmlNode node, MyNamespace.MyObject myObject, String str) { ... I've tried using delegates, and also MethodInfo, but...
10
2477
by: Tony Rice | last post by:
If I've loaded a set of functions in a webdocument like this: <script language="JavaScript" type="text/javascript" src="/js/foo.js"</script> Shouldn't I be able to call a function from inside foo.js later in that same document by doing something like this: <script language="JavaScript" type="text/javascript">myfunction(myargs)
2
1558
by: hemant.singh | last post by:
I am stuck with strange JS issue My product gives user to show some images(which track mouseover) on page by embedding script like <script src=http://domain.com/GetDynamicJS?domagic=1> </script> Now user is embedding more than of the above and when user mouse over's the image output of first1 than function getting called is script loaded in last ... thus error, as that script don't have the relevant
5
5021
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
2
1957
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 ================================================
22
3468
by: Archanak | last post by:
Hi, I am using 2-level CSS Drop Down Menu in my perl/CGI program. here is the code. #!c:/perl/bin/perl.exe use CGI qw(:standard);
0
9645
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
9480
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,...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8974
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...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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

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.