Unable to make imports to work

Unable to make imports to work

Hello all. 

I am creating a small form based in a given json, but my dynamic imports are not working properly, it add only after the function is called causing a function to not be found.

In order to make this import I did this small function:

  1. function include(scriptName){
  2.     var head = document.getElementsByTagName('head')[0];
  3.    
  4.     script = document.createElement('script');
  5.     script.src = scriptName;
  6.     script.type = 'text/javascript';
  7.    
  8.     head.appendChild(script);
  9. }

It works as it should, but the order is not being respected here. In my ready function I did:

  1. $(document).ready(function(){
  2. // import required scripts
  3. importRequirements();

  4. // 1. grab panel that will receive groups
  5. $checklistPanel = $("#checklist-form");
  6. // This setter will be here ONLY because the mock, must be removed once all is ok
  7. setCurrentCheckList("Remove after this after checklist is done");

  8. // 2. now, start creating groups
  9. createGroups($currentCheckList.groups, $checklistPanel);

  10. // 3. after group creation, transform it into an accordion
  11. $checklistPanel.accordion({
  12. collapsible: true
  13. });
  14. });
The import is there if I check the html in chrome console, but at the sources tab, it is added only after the exception is fired.

I tried adding it to document onload, the result was the same, it loads only after the method createGroups has been runned once.

Is there something that I can do without having to add it to the header? There are 9 scripts, and they will always be used when this script is used, that is why I am trying to add it programatically.