如何通过JavaScript文件将所有JavaScript文件包含在目录中? [英] How can I include all JavaScript files in a directory via JavaScript file?

查看:99
本文介绍了如何通过JavaScript文件将所有JavaScript文件包含在目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面中包含一堆JavaScript文件,但我不想继续编写

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

<script type="text/javascript" src="js/file.js"></script>

那么有没有办法在目录中包含所有文件(未知大小)?我可以做类似......

So is there a way to include all files in a directory (unknown size)? Can I do something like...

$.getScript("js/*.js");

...获取js目录中的所有JavaScript文件?我怎么能用jQuery做到这一点?

... to get all the JavaScript files in the "js" directory? How can I do this using jQuery?

推荐答案

一般来说,这可能不是一个好主意,因为你的html文件应该只是正在加载他们实际使用的JS文件。无论如何,这对于任何服务器端脚本语言来说都是微不足道的。只需在将页面提供给客户端之前插入脚本标记。

In general, this is probably not a great idea, since your html file should only be loading JS files that they actually make use of. Regardless, this would be trivial to do with any server-side scripting language. Just insert the script tags before serving the pages to the client.

如果要在不使用服务器端脚本的情况下执行此操作,可以将JS文件放入目录中允许列出目录内容,然后使用XMLHttpRequest读取目录的内容,并解析出文件名并加载它们。

If you want to do it without using server-side scripting, you could drop your JS files into a directory that allows listing the directory contents, and then use XMLHttpRequest to read the contents of the directory, and parse out the file names and load them.

选项#3是要有一个loaderJS文件,它使用getScript()加载所有其他文件。将它放在所有html文件的脚本标记中,然后只需在上传新脚本时更新加载程序文件。

Option #3 is to have a "loader" JS file that uses getScript() to load all of the other files. Put that in a script tag in all of your html files, and then you just need to update the loader file whenever you upload a new script.

这篇关于如何通过JavaScript文件将所有JavaScript文件包含在目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆