By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

Dynamically loading external javascript files.

P: n/a
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.

Apr 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
oz*********@gmail.com 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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 14 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.