一个 javascript 文件的多个来源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11493586/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:24:30  来源:igfitidea点击:

Multiple sources for a javascript file

javascripthtml

提问by Amanjot Singh

I am working on a project where I need to include somewhat around 10-15 .js files in the HTML head section directly like

我正在开发一个项目,我需要在 HTML 头部部分直接包含大约 10-15 个 .js 文件,例如

<script type="text/javascript" src="http://localhost:9020/website1/wa/min/soundmanager2.js,vars.js,utils/md5.js,utils/utils.js></script>

what is the way I can give refrences correctly

我可以正确给出参考的方法是什么

the files I need to refre are in the same hierarchy like

我需要引用的文件在相同的层次结构中

1.....2,3 2.........4,5 3........6,7

1.....2,3 2.........4,5 3........6,7

I need to refer 1,4,7 please help.

我需要参考 1,4,7 请帮忙

somewhere I read this method what's it?

我在某处读到这个方法是什么?

<script type="text/javascript" src="http://localhost:9020/wordplus/root/child/?b=scripts&f=soundmanager2.js,vars.js,utils/md5.js,utils/utils.js></script>

回答by KGZM

The example you posted looks exactly like the query string interface for the minify PHP library: http://github.com/mrclay/minify

您发布的示例看起来与 minify PHP 库的查询字符串接口完全一样:http: //github.com/mrclay/minify

Using it you use it in the fashion <script src="min/?b=path&f=script1.js,script2.js,ui/script3.js"></script>. Where pathis the path from your web root that you want it to look for scripts under.

使用它,您就可以时尚地使用它<script src="min/?b=path&f=script1.js,script2.js,ui/script3.js"></script>path您希望它在其下查找脚本的 Web 根路径在哪里。

I've used it before and I've found it quite effective. It concatenates, minifies, caches, and serves JS and CSS.

我以前用过它,我发现它非常有效。它连接、缩小、缓存并提供 JS 和 CSS。

I'm sure there are other libraries to achieve the same effect, alternatively you can create a build script to concatenate and minify all your scripts and then deploy a single JS file to your site, in a single script tag.

我确信还有其他库可以实现相同的效果,或者您可以创建一个构建脚本来连接和缩小所有脚本,然后在单个脚本标记中将单个 JS 文件部署到您的站点。

回答by freefaller

It is not possible to load multiple javascript files in a single <script>element.

不可能在单个<script>元素中加载多个 javascript 文件。

You have to have to have an individual <script>element for each script you are referencing..

您必须为<script>您引用的每个脚本拥有一个单独的元素。

<script type="text/javascript"
  src="http://localhost:9020/wordplus/root/child/?b=scripts&f=soundmanager2.js"></script>
<script type="text/javascript"
  src="http://localhost:9020/wordplus/root/child/?b=scripts&f=vars.js"></script>

回答by Dennis

The second line you posted requests scripts from the server dynamically. the bparameter of the request tells it you want scripts and the fparameter tells the server which files you want. Then it concatenates these into one file and sends that back to the user agent. You need server-side scripting to handle this; it is not something built into the URL specification.

您从服务器动态发布请求脚本的第二行。b请求的参数告诉它你想要脚本,f参数告诉服务器你想要哪些文件。然后它将这些连接成一个文件并将其发送回用户代理。您需要服务器端脚本来处理这个问题;它不是内置于 URL 规范中的内容。

http://localhost:9020/wordplus/root/child/
b=scripts
f=soundmanager2.js,vars.js,utils/md5.js,utils/utils.js

The simplest solution is just have one script tag per file as it will let you take advantage of caching:

最简单的解决方案是每个文件只有一个脚本标签,因为它可以让您利用缓存:

<script src="http://localhost:9020/wordplus/root/child/soundmanager2.js"></script>
<script src="http://localhost:9020/wordplus/root/child/vars.js"></script>
<script src="http://localhost:9020/wordplus/root/child/utils/md5.js"></script>
<script src="http://localhost:9020/wordplus/root/child/utils/utils.js"></script>

回答by davidbuzatto

Another solution is to use some JavaScript builder to join all your files, generating just one. The pros of this approach is that your result file will be "compressed" (minified). Some builders:

另一种解决方案是使用一些 JavaScript 构建器来连接所有文件,只生成一个。这种方法的优点是您的结果文件将被“压缩”(缩小)。一些建设者: