Is there any function to include all JavaScript files from a folder using JavaScript. Since I'm have plenty if .js file to include.
-
1No, there isn't. But you can load the modules using ocLazyLoad or requirejsAlon Eitan– Alon Eitan2016-04-24 04:09:06 +00:00Commented Apr 24, 2016 at 4:09
-
2Possible duplicate of How can I include all JavaScript files in a directory via JavaScript file?ManoDestra– ManoDestra2016-04-24 04:15:25 +00:00Commented Apr 24, 2016 at 4:15
Add a comment
|
2 Answers
This is a really brief way to require all files (node.js-style):
var fs = require('fs');
// assuming all things are files
fs.readdirSync('path/to/dir').forEach(function(elementName) {
require('path/to/dir' + elementName);
});
Generally though people will make an index file in the root of the folder that individually requires each file and exports the appropriate fields.
2 Comments
Hulke
he can do it with any lang. that have access to the file-system, that wasn't the question though... by the question and tags seems like he is looking for a way to access it from the client
Catalyst
I wasn't totally sure, even though he mentioned angular. Thought I'd throw this out there in case it was helpful.
There isn't, as JS cannot read the file system. You could:
- Create an array of filenames
- Create a php (or other server-side) script that returns the file list and fetch the list with ajax
Once you have the list of files, you can load them with JS:
var js=document.createElement("script");
js.src=url;
document.body.appendChild(js);
1 Comment
Alon Eitan
It's an angularJs question, your answer is irrelevant