1

I sometimes need to load my JS libraries like this so I always get the current version...

<script src="https://cdn.domain.com/file.js"></script>

However, my current issue, is I need to load this file in my Javascript code, not through HTML.

PseudoCode:

waitforload(getscript('https://cdn.domain.com/file.js'));

I need to load the file and then wait until loaded to proceed on to additional code that uses the library (file.js)

What is the best way to do this? I can't seem to find a simple and good solution.

Not a duplicate

As both solutions recommends using callbacks. I do not want to use a callback to execute code after it's run. I want the library to load, then move on to the next code to execute.

How do I include a JavaScript file in another JavaScript file?

Dynamically load JS inside JS

Example:

 waitforload(getscript('https://cdn.domain.com/file.js'));

 el = new Helpers.Domain.fromFileJs();
 el.something();

Vs.

 waitforload(getscript('https://cdn.domain.com/file.js', 'mycode()'));

function my code()
  el = new Helpers.Domain.fromFileJs();
  el.something();
end

As to me, the first version easier to deal with as I am loading a module full of helpers classes and other code, and it's not just one item.

asachet
  • 6,620
  • 2
  • 30
  • 74
user2012677
  • 5,465
  • 6
  • 51
  • 113
  • 1
    _"As both solutions recommends using callbacks. I do not want to use a callback to execute code after it's run. I want the library to load, then move on to the next code to execute."_ What do you think a callback does? – j08691 Jul 01 '19 at 15:08
  • It's the implementation. In one, I have to do: some function(callback);, the other, I run code on one line, then run code on next line. I don't need to wait for a response, just that it loads. – user2012677 Jul 01 '19 at 15:09
  • call your external library in the head tag and call the other js file at the bottom just before the closing body tag – Running Buffalo Jul 01 '19 at 18:42
  • I am testing in Teaspoon-Jasmine, can't do that. It does not work. – user2012677 Jul 01 '19 at 18:44

0 Answers0