473,385 Members | 1,863 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,385 software developers and data experts.

(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.getElementsByTagName('head').item(0);

var file = document.createElement('script');
file.setAttribute('language','javascript');
file.setAttribute('type','text/javascript');
file.setAttribute('src','/include.js');
html_doc.appendChild(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 2192


sf****@gmail.com 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.createElement('script');
script.addEventListener(
'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.getElementsByTagName('head')[0].appendChild(script);

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 14 '06 #2
Martin Honnen wrote:
var script = document.createElement('script');
script.addEventListener(
'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('onload' ... and
YAHOO.util.Event.addListener( script,'load',callback);
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.com wrote:
IE6 seems to be a different story though.
I tried both the equivalent with attachEvent('onload' ... and
YAHOO.util.Event.addListener( script,'load',callback);
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.createElement('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.com wrote:
IE6 seems to be a different story though.
I tried both the equivalent with attachEvent('onload' ... and
YAHOO.util.Event.addListener( script,'load',callback);
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.createElement('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 "onreadystatechange" instead.

Thanks,
Sebastian

Jun 14 '06 #5
the DtTvB wrote:
sfe...@gmail.com wrote:
IE6 seems to be a different story though.
I tried both the equivalent with attachEvent('onload' ... and
YAHOO.util.Event.addListener( script,'load',callback);
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.createElement('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.onreadystatechange = 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
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...
5
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...
9
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...
4
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...
10
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...
2
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>...
5
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
by: sylver | last post by:
Hi, Following the discussion that can be found here:...
22
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.