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.
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/