使用for循环在html中包含多个脚本文件

I have a folder with multiple .js files(I have the file number count in a variable)

Is it possible to "include" all the js scripts using a for loop?

Something like:

for(i = 0; i <= file_count; i++) {
   include('scriptname' + i)
}

Thanks

declare a php array inside a php file (call it "file_names.php") and write names of all .js files

<?php 

$file_array=array('file1.js','file2.js')
?>

then include the php file in your desired page and iterate over the array inside your page's HTML header tag

include('file_names.php')

<?php foreach($file in $file_array){ ?>

<script src='<?php echo $file; ?>'></script>
<?php } ?>

Try this: include('scriptname' + i + '.js'); if filenames are of form scriptname0.js, scriptname1.js and so on! Also remember to call this PHP for loop inside a HTML <script> tag.

There are a number of options available to you. If you are using jQuery (other frameworks offer equal functionality) then there is a method called getScript which will likely suit your needs. If you are doing this manually then you will need to create script elements using document.createElement and insert them into the head or body as appropriate.

You are advised to try and use a helper function though as there are a number of considerations when loading external resources, not least of which is ensuring they have actually loaded before attempting to make use of them.

Yes you can add the js files to head section as below

    for(i = 0; i <= file_count; i++) {
    var jsfile = 'scriptname' + i ;  
    $('head').append($('<script>').prop('type', 'text/javascript').prop('src', jsFile)); 
    }

If you have the file names of script, then Yes you can like this,

for(i = 0; i <= file_count; i++) {
   var script = document.createElement( "script" );
   script.type = "text/javascript";
   script.src = "scriptname"+i+".js"; //whatever the filename
   $("head").append(script);
}