Problems with dynamic loading in JavaScript

后端 未结 3 738
执笔经年
执笔经年 2021-01-05 21:47

I am a JavaScript newbie and learn by working on a pure JavaScript \"project\" that calculates mathematical functions. It all works well. Now, as a further step, I

3条回答
  •  长发绾君心
    2021-01-05 22:11

    When you add the script tag to your document, it is not loaded synchronously. You need to wait for the file to be loaded before you can use the code that was in it.

    you may be able to redesign your code to use a script.onload callback:

    var reference = document.createElement('script');
    // ...
    reference.onload = function() {
      alert("Script loaded and ready");
    };
    

    but for this scenario, if you don't have many language string you may be best to just load them all statically.

提交回复
热议问题