简体   繁体   中英

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!

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.

您可以使用requireJS ,将文件合并在一起或使用Jquery.getScript()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM