简体   繁体   中英

How to load everything from one folder?

Let's say I have folder scripts with 10 javascript files and instead of doing this:

<script src="scripts/js1.js"></script>
<script src="scripts/js2.js"></script>
<script src="scripts/js3.js"></script>
<script src="scripts/js4.js"></script>
<script src="scripts/js5.js"></script>
//and so on...

I want to do this:

<script src="scripts/*"></script>

which load ALL files in scripts

How would I go about doing this?

Won't be as easy as that, here is what you are looking for:

How can I include all JavaScript files in a directory via JavaScript file?

What you try to achieve is cumbersome from the client side. You can achieve the same through server-side scripting before you load your html file. You can use a task runner tool like grunt , gulp etc. (or write a script) that will traverse your target directory, retrieve all the *.js file paths and append their script tags in your html file.

Another solution is to use a tool to concatenate all your js files in one bundle file and only load that file from your html file. There are plenty of tools out there to do that

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