如何在 JavaScript 中导入/包含源文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11248657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import/include source files in JavaScript?
提问by Michael
Possible Duplicate:
How to include js file in another js file?
可能的重复:
如何在另一个 js 文件中包含 js 文件?
Suppose I have a few JavaScript source files: a.js
, a1.js
, and a2.js
. Functions of a.js
invoke functions from a1.js
and a2.js
.
假设我有几个JavaScript源文件:a.js
,a1.js
,和a2.js
。的功能a.js
从调用函数a1.js
和a2.js
。
Now I have to declare allthese files in my HTML page. I would like to declare only a.js
in the HTML and "import/include" a1.js
, and a2.js
in the a.js
source file.
现在我必须在我的 HTML 页面中声明所有这些文件。我想声明只a.js
在HTML和“导入/包括” a1.js
,而a2.js
中a.js
源文件。
Does it make sense? Can I do that in JavaScript?
是否有意义?我可以在 JavaScript 中做到这一点吗?
采纳答案by Denys Séguret
You can't specify imports in vanilla javascript.
您不能在 vanilla javascript 中指定导入。
So your solutions (excluding heavy server side frameworks) are :
所以你的解决方案(不包括重型服务器端框架)是:
simply do the imports
concatenate your js files (and minify them in the same move, for exemple using the closure compiler)
use a module itool like require.js
简单地做进口
连接你的 js 文件(并在同一个动作中缩小它们,例如使用闭包编译器)
使用像require.js这样的模块工具
As long as you're not experienced, and if your number of files is low (less than 15), I recommend to simply choose the first or second solution. Using a module loader may have side effects you don't want to debug when beginning to learn javascript.
只要您没有经验,并且如果您的文件数量很少(少于 15 个),我建议您只需选择第一个或第二个解决方案。在开始学习 javascript 时,使用模块加载器可能会产生您不想调试的副作用。
回答by Israelm
You can import:
您可以导入:
<script type="text/javascript"src="a1.js"></script>
<script type="text/javascript"src="a2.js"></script>
<script type="text/javascript"src="a3.js"></script>
if you want to do it directly from the JS, you may use Ajax, this post explains how to: include-javascript-file-inside-javascript-file
如果你想直接从 JS 做,你可以使用 Ajax,这篇文章解释了如何: include-javascript-file-inside-javascript-file
回答by Eric J.
You can bundle JavaScript (and also CSS) files together using certain tools to reduce the number of files you must include. This also increases page load performance.
您可以使用某些工具将 JavaScript(以及 CSS)文件捆绑在一起,以减少必须包含的文件数量。这也提高了页面加载性能。
These tools combine multiple JavaScript files into a single JavaScript file (optionally minifying the files as well), and multiple CSS files into a single CSS file. This results in fewer HTTP connections from the browser to the server, so there are fewer things to be fetched serially.
这些工具将多个 JavaScript 文件合并为一个 JavaScript 文件(也可以选择缩小文件),并将多个 CSS 文件合并为一个 CSS 文件。这导致从浏览器到服务器的 HTTP 连接更少,因此需要串行获取的内容更少。
ASP.Net MVC 4 has built-in support for this:
ASP.Net MVC 4 对此有内置支持:
http://theshravan.net/bundling-and-minification-support-in-asp-net-mvc-4/
http://theshravan.net/bundling-and-minification-support-in-asp-net-mvc-4/
There are a number of solutions for other environments as well such as Juicer.
其他环境也有许多解决方案,例如Juicer。
If you cannot bundle all resources (perhaps some come from a CDN while others are served locally), you can use a load manager such as require.js.
如果您不能捆绑所有资源(也许一些来自 CDN,而其他资源在本地提供),您可以使用诸如require.js 之类的负载管理器。