2

Is there any function to include all JavaScript files from a folder using JavaScript. Since I'm have plenty if .js file to include.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
ArunJaganathan
  • 695
  • 2
  • 7
  • 20
  • 1
    No, there isn't. But you can load the modules using [ocLazyLoad](https://github.com/ocombe/ocLazyLoad) or [requirejs](http://requirejs.org/) – Alon Eitan Apr 24 '16 at 04:09
  • 2
    Possible duplicate of [How can I include all JavaScript files in a directory via JavaScript file?](http://stackoverflow.com/questions/4175008/how-can-i-include-all-javascript-files-in-a-directory-via-javascript-file) – ManoDestra Apr 24 '16 at 04:15

2 Answers2

0

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.

Catalyst
  • 3,143
  • 16
  • 20
  • 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 – Hulke Apr 24 '16 at 07:04
  • I wasn't totally sure, even though he mentioned angular. Thought I'd throw this out there in case it was helpful. – Catalyst Apr 24 '16 at 07:15
-1

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);
Gabriel
  • 2,170
  • 1
  • 17
  • 20