Dynamically loading external javascript files.

O

ozzy.osborn

Hello All,

I have been struggling with a cross browser solution to loading
external javascript files on the fly.

I have been successful using the following code in IE6:

var newScr = document.createElement("SCRIPT");
newScr.src = "newScr.js";
newScr.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(newScr);

I believe the reason is that IE is loading the external file
syncronously while Firefox is not. Is there an onload event for
creating an element (if so I do not see it in Venkman). I have seen the
solution of using XMLHTTP to load the script but I am trying to get
around any dependency (atleast at this stage of the library) on
activex.

Thanks in advance for any help.
 
R

Randy Webb

(e-mail address removed) said the following on 4/14/2006 3:49 PM:
Hello All,

I have been struggling with a cross browser solution to loading
external javascript files on the fly.

What is the problem you are having?
I have been successful using the following code in IE6:

var newScr = document.createElement("SCRIPT");
newScr.src = "newScr.js";
newScr.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(newScr);

That code is successful in Opera and Mozilla as well.
I believe the reason is that IE is loading the external file
syncronously while Firefox is not. Is there an onload event for
creating an element (if so I do not see it in Venkman).

You could try newScr.onload = itsLoaded;

Works in Opera9 and Firefox but not in IE6.

What it sounds like you are trying to do is something like this:

var newScr = document.createElement("SCRIPT");
newScr.src = "newScr.js";
newScr.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(newScr);

//code here to do something with the code in newScr.js

If that is true, the simplest most reliable way is to have newScr.js to
let you know when its loaded:

newScr.js:
//data here
functionCall();

Where functionCall() is in the main page that is loading the document.
Then, that function never gets called until after the data is loaded.
It's a timing issue.
I have seen the solution of using XMLHTTP to load the script but
I am trying to get around any dependency (atleast at this stage
of the library) on activex.

XMLHTTPRequest is probably the *worst* way to accomplish dynamic script
loading.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top