xShirase
xShirase

Reputation: 12389

JS : Including libraries for an external script

Ok, this one should be a no brainer for most of you :

I have made a myclock.js script that i include in my html between <script></script> This script requires moment.js. Do I have to add the reference library in my html file between the script markup or can I load it from my external script somehow? If the latter, how do I do that?

In code, in case I'm not clear enough :

Should I do :

<script type="text/javascript" src=".../moment.js"></script>
<script type="text/javascript" src="myclock.js"></script>

OR better do :

<script type="text/javascript" src="myclock.js"></script>

and include moment.js in my script (it already has the line require(moment); )?

Thanks!

Upvotes: 0

Views: 185

Answers (2)

Software Guy
Software Guy

Reputation: 3280

You cannot load another JavaScript file using standard JavaScript. The require() method is not standard JavaScript. So I would use the first method. Of course, there are ways that people have developed to modularize JavaScript with the ability to load js files, but for that you need to use those respective libraries.

Upvotes: 1

Reko
Reko

Reputation: 98

You can use requireJS , merge the files together or use Jquery.getScript()

Upvotes: 0

Related Questions