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

后端 未结 10 1394
说谎
说谎 2020-12-03 00:42

I have a bunch of JavaScript files that I would like to include in the page, but I don\'t want to have to keep writing



        
10条回答
  •  无人及你
    2020-12-03 01:29

    Given that you want a 100% client side solution, in theory you could probably do this:

    Via XmlHttpRequest, get the directory listing page for that directory (most web servers return a listing of files if there is no index.html file in the directory).

    Parse that file with javascript, pulling out all the .js files. This will of course be sensitive to the format of the directory listing on your web server / web host.

    Add the script tags dynamically, with something like this:

    function loadScript (dir, file) {
     var scr = document.createElement("script");
     scr.src = dir + file;
     document.body.appendChild(scr);
     }
    

提交回复
热议问题