Dynamic script addition should be ordered?

后端 未结 6 1992
花落未央
花落未央 2020-12-16 04:31

I\'m adding some

相关标签:
6条回答
  • 2020-12-16 05:07

    Sorry for answering my own question, but its been a while and we did come up with a solution. What we came up with was to load the javascript concurrently as text contained in a json object, and then used eval() once they were all loaded to execute them in the correct order. Concurrent load plus ordered execution. Depending on your use case you may not need the json. Roughly, here is some code that shows what we did -

    // 'requests' is an array of url's to javascript resources
    var loadCounter = requests.length;
    var results = {};
    
    for(var i = 0; i < requests.length; i++) {
       $.getJSON(requests[i], function(result) {
          results[result.id] = result;
          ...
          if(--loadCounter == 0) finish();
       });
    }
    
    function finish() {
      // This is not ordered - modify the algorithm to reflect the order you want
      for(var resultId in results) eval(results[resultId].jsString);
    }
    
    0 讨论(0)
  • 2020-12-16 05:09

    The download order and the execution order is not the same thing. In your page, even if B.js is downloaded first, the browser's engine will wait for A.js to continue processing the page.

    The scripts are definitely processed, not only in the order they appeared in the document, but also at the place they appeared.

    Imagine if it wouldn't be like that, there would be many errors if your little script that uses jQuery is downloaded and processed before the jQuery library.

    Also, when you do a "document.write" in a js file, it appears where the script has been declared. You can't access DOM objects that are appearing after the script declaration neither.

    This is why there are recommendations to put scripts at the very bottom of the page, to prevent their execution too soon and decrease the "perceived load time" of the page, because the browser's rendering engine is stopped as soon as a script is processed.

    Mike

    EDIT: if they are added dynamically with javascript, I think they are processed in the order they were added in time.

    0 讨论(0)
  • 2020-12-16 05:16

    As I understand it, they are meant to be executed in the order they appear in the document. Some browser might be able to perform some parsing out of order, but they would still have to be executed in the correct order.

    0 讨论(0)
  • 2020-12-16 05:16

    You could load b.js from a.js to be 100% sure ... although I'd like the definitive answer to this question myself, especially with sync ajax loading of scripts.

    0 讨论(0)
  • 2020-12-16 05:16

    I was investigating this while working on a little library that loads modules dynamically like YUI 3. I created a little test here that loads two scripts that just insert content into divs. One is a common JS file and the other is a PHP file that waits 3 seconds to execute.

    http://www.juandopazo.com.ar/tests/asyn-script-test.html

    As you can see, scripts are executed when they finish loading, and not in the order in which you append them to the DOM, in every browser.

    0 讨论(0)
  • 2020-12-16 05:27

    No, you cannot expect that all browsers will defer execution of both scripts until both are loaded (**especially when you are adding them dynamically).

    If you want to execute code in B.js only after A.js is loaded then your best bet is to add an onload callback to A.js that sets a variable and another one to B.js that checks to see if that variable has been set, then it executes the necessary function in B.js if it has (and if A.js has not loaded, it starts a timer that periodically checks until it has loaded).

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