Is there a known workaround for IE9's execution order of injected script tags?

后端 未结 4 1924
無奈伤痛
無奈伤痛 2021-01-14 06:18

I am sure I don\'t fully understand this problem, but it seems that we are seeing strange behavior on IE9 on my project, somehow related to out-of-order execution o

相关标签:
4条回答
  • 2021-01-14 06:34

    I have made a little script, exactly for this purpose:

    https://github.com/mudroljub/js-async-loader

    In short, it loads all your scripts asynchronously, and then executes them consequently. It looks something like this:

    for (var lib in libs) {
        loadAsync(lib);
    }
    

    And you don't need document.write();

    0 讨论(0)
  • 2021-01-14 06:37

    I guess you could chain the onload event of one to start the load of another:

    var newJS= document.createElement('script');
    newJS.onload=function() {alert("done")} //or call next load function
    newJS.src="..."
    document.body.appendChild(newJS)
    
    0 讨论(0)
  • 2021-01-14 06:43

    So the advantage of writing script tags this way is that they are loaded asynchronously. I don't know about browser nuances about exactly how this is done but I would have thought they would be executed when they're downloaded, in no specific order. Similar to the behaviour of the HTML5 async attribute.

    There's another HTML5 attribute defer which instead makes scripts execute in order, but in a non blocking way. You could try adding that into your generated <script> tags. IE9 partially honours it.

    0 讨论(0)
  • 2021-01-14 06:57

    Use a script loader (like the one I wrote: LABjs), which will normalize all the different quirks of loading across the various browsers. And bonus: it doesn't use that god-awful document.write(). LABjs will let you load all your scripts asynchronously (in parallel), but make sure they execute in the proper order. Sounds like basically exactly what you want.

    0 讨论(0)
提交回复
热议问题