Dynamic (2 levels) Javascript/CSS Loading IE6

2019-09-16 13:15发布

问题:

i am trying to dynamically include js (and css) files into a webpage like this: index.html -> loader_a.js -> a_foo.js, a_bar.js, a_foo.css and so on.
While this works without a problem in FF (using appendChild) i cant get it to run in IE6. I've tried various available solutions (adding to dom node, ajax call and eval and more from (http://ntt.cc/2008/02/10/4-ways-to-dynamically-load-external-javascriptwith-source.html) here and there and others like post #2013676) but it's not doing what its supposed to do.

When i check with DebugBar i see that my include files (eg a_foo.js) is actually loaded, but its content is empty - on other included files (1 Level/directly) this content is show so i assume there is the problem ...

The "error" i get is alway undefined object which is o/c b/c the function i call is not loaded properly so not much of a help. I dont get any errors on the includes.
I've validated the javascripts so those whould be ok.

Does anyone have the ultimate solution for this? I can recreate my tests and post some code if it helps.

Thanks, regards, Thomas

Sample HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<HTML lang=en><HEAD><TITLE>Test</TITLE>

<script type="text/javascript" src="mmtest_files/jquery-1.4.2.min.js"></script>

<script type="text/javascript" src="mmtest_files/multiload.js"></script>
<script type="text/javascript" >

function init2() {
  // using the data from the loaded js files
  var a= mmf("a");
  document.getElementById('status').innerHTML = "Variable set:" + a;

}

// magic...
include(['mmt.js'],init2);

</script>

<BODY >

<H2>Test me!</H2>
<SPAN id=status>status old</SPAN>

</BODY></HTML>

JS 1 is multiload from answer 1

JS2 is a test include:

function mmf(param) { return "Called with" + param; }

回答1:

You need to use document.write in ie, in order to load scripts in parallel.

See: Loading Scripts Without Blocking

I have such a script btw: Loading Multiple Javascript Files In Order Asynchronously

(it may need some enchancements in Chrome)


UPDATE

There is a callback function, it is optional. It can be used to couple dependent script to the files. EG:

function myjQueryCode() {
   // ...
}

include(['jquery.js','jquery-ui.js'], myjQueryCode);

So that your jquery dependent code will run after the files has been loaded.