dynamically loading and using javascripts in firefox

S

stroumf

Hey,

My problem is the following:
I need to use functions from different javascripts and I don't want to
load all the javascripts initially. So I need to load the scripts on
demand.
Now I know how I can load a script dynamically:
function loadScript(scriptname) {
var e = document.createElement("script");
e.src = scriptname+".js";
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
}
in script.js I have a function test()
Now I have a function where I first check whether the required script
is loaded and if not I call the function above (loadScript). After the
check (and possibly load) and excecute the function from the loaded
script.
Now in firefox this is a problem, cause firefox is still loading the
script while I try to excecute the function from that script. How can I
solve this problem?

thanx in advance
 
M

Martin Honnen

stroumf said:
function loadScript(scriptname) {
var e = document.createElement("script");
e.src = scriptname+".js";
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
}

Now in firefox this is a problem, cause firefox is still loading the
script while I try to excecute the function from that script. How can I
solve this problem?

Use an event listener that is called when the script has been loaded
e.addEventListener(
'load',
function (evt) {
// now call your function here
},
false
)
Do that before you call the appendChild that inserts the script element
in the document.
 
R

Randy Webb

stroumf said the following on 11/8/2006 11:06 AM:
Hey,

My problem is the following:
I need to use functions from different javascripts and I don't want to
load all the javascripts initially. So I need to load the scripts on
demand.
Now I know how I can load a script dynamically:
function loadScript(scriptname) {
var e = document.createElement("script");
e.src = scriptname+".js";
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
}
in script.js I have a function test()
Now I have a function where I first check whether the required script
is loaded and if not I call the function above (loadScript). After the
check (and possibly load) and excecute the function from the loaded
script.
Now in firefox this is a problem, cause firefox is still loading the
script while I try to excecute the function from that script. How can I
solve this problem?

Firefox2.0 and Opera9 support the onload handler for a new script
element. IE7 doesn't trigger it (IIRC, IE6 didn't either but I don't
recall for sure). So, you could add an onload to the script element:

function loadScript(scriptname) {
var e = document.createElement("script");
e.src = scriptname + ".js";
e.type="text/javascript";
e.onload = function (){alert('Its loaded')}
document.getElementsByTagName("head")[0].appendChild(e);
}
 

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,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top