javascript 在 <script> 标签的 src 属性中使用通配符?

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

Use wildcard in src attribute of <script> tag?

javascripthtmlwildcardscript-tag

提问by El Ronnoco

Ok, stupid question and I don't think it's possible but, I have this markup at the top of my .aspx page...

好吧,愚蠢的问题,我认为这是不可能的,但是,我的 .aspx 页面顶部有这个标记......

<%--Third Party Libraries, Plugins, Extensions --%>
<script src="Libraries/Raphael/Raphael.js" type="text/javascript"></script>
<script src="AutoComplete/jquery.autocomplete.js" type="text/javascript"></script>    
<script src="Libraries/jQuery/1.4.2/jquery.js" type="text/javascript"></script>
<script src="Libraries/jQuery/UI/1.8.4/jquery.ui.core.js" type="text/javascript"></script>
<script src="Libraries/jQuery/UI/1.8.4/jquery.ui.widget.js" type="text/javascript"></script>
<script src="Libraries/jQuery/UI/1.8.4/jquery.ui.mouse.js" type="text/javascript"></script>
<script src="Libraries/jQuery/UI/1.8.4/jquery.ui.draggable.js" type="text/javascript"></script>
<script src="Libraries/jQuery/UI/1.8.4/jquery.ui.droppable.js" type="text/javascript"></script>    

Wouldn't it be nice if I could replace that with this...

如果我能用这个代替它不是很好吗......

<%--Third Party Libraries, Plugins, Extensions --%>
<script src="Libraries/Raphael/Raphael.js" type="text/javascript"></script>
<script src="AutoComplete/jquery.autocomplete.js" type="text/javascript"></script>    
<script src="Libraries/jQuery/1.4.2/jquery.js" type="text/javascript"></script>
<script src="Libraries/jQuery/UI/1.8.4/jquery.ui.*.js" type="text/javascript"></script>

ie use the *as wildcard.

即使用*作为通配符。

Obviously as this is JS I could just throw all those scripts into one big script and load that but I don't really fancy doing that.

显然,因为这是 JS,我可以将所有这些脚本放入一个大脚本中并加载它,但我并不喜欢这样做。

Anyone else have a technique for tidying up masses of script refs? Or do we just live with it?

其他人有整理大量脚本参考的技术吗?或者我们只是忍受它?

采纳答案by xzyfer

As far as I know this is not possible, simply because, the browser would need to know exactly what files to request.

据我所知这是不可能的,仅仅是因为浏览器需要确切地知道要请求哪些文件。

The browser would essentially have to brute force your server with requests hoping to get lucky.

浏览器本质上必须通过希望获得幸运的请求来强制您的服务器。

I'd suggest using Google's closure compiler to merge all similar, if not all, javascript files into a single file. It will be slightly large, but would cut down on http request.

我建议使用 Google 的闭包编译器将所有类似的(如果不是全部)javascript 文件合并到一个文件中。它会稍微大一点,但会减少 http 请求。

With some profiling you could find a balance between which files are needed most commonly and speed.

通过一些分析,您可以找到最常用的文件和速度之间的平衡。

UPDATE (from comments)

更新(来自评论)

I'm generally reluctant to offer adding a new javascript library to solve the issue of too many javascript libraries :) Plus this just seemed like the more straight forward solution. Current we use the Google closure API to compress and merge all our javascript and CSS and build time with ANT. Works a charm. This can also be done to some extent direct with apache2 virtual host/htaccess (see html5boilerplate.com) for examples and limitations

我一般不愿意提供添加一个新的 javascript 库来解决太多 javascript 库的问题 :) 另外,这似乎是更直接的解决方案。目前,我们使用 Google 关闭 API 来压缩和合并我们所有的 javascript 和 CSS,并使用 ANT 构建时间。很有魅力。这也可以在某种程度上直接使用 apache2 虚拟主机/htaccess(参见 html5boilerplate.com)来完成示例和限制

回答by David Tang

Nope, not in the way you're thinking of. You coulddo something that's similar if you're using JavaScript loaders (e.g. RequireJSor LabJS), since you can then condense each file into an array and loop through them, or, if you're feeling ambitious cook up some protocol between the front and back ends to support this.

不,不是你想的那样。如果您使用 JavaScript 加载器(例如RequireJSLabJS),您可以做一些类似的事情,因为您可以将每个文件压缩成一个数组并循环遍历它们,或者,如果您有雄心,可以在前端之间编写一些协议和后端来支持这一点。

Nonetheless, this is not recommended as it's not easy to maintain. If your problem with combining the files into a single one is with the effort, then JS minifiers (e.g. UglifyJSor Closure Compiler) may solve your problem.

尽管如此,不建议这样做,因为它不容易维护。如果您在将文件合并为一个文件时遇到问题,那么 JS 压缩器(例如UglifyJSClosure Compiler)可能会解决您的问题。

回答by Quick Joe Smith

Actually, as somebody else may have mentioned, you could add your own script file in there and do something like add the paths to an array, loop through it calling getScriptfor each item.

实际上,正如其他人可能提到的那样,您可以在其中添加自己的脚本文件并执行一些操作,例如将路径添加到数组中,循环遍历它,为每个项目调用getScript

$.getScript('ajax/test.js', function() {
    alert('Load was performed.');
});

回答by Quentin

ie use the * as wildcard.

即使用 * 作为通配符。

No. Well, not unless you want to configure your server to pass a URL which includes a *character through a script that resolves it and bundles up all the JS on the fly.

不。好吧,除非您想配置您的服务器以*通过一个脚本传递一个包含字符的 URL,该 URL解析它并动态捆绑所有 JS。

Obviously as this is JS I could just throw all those scripts into one big script and load that but I don't really fancy doing that.

显然,因为这是 JS,我可以将所有这些脚本放入一个大脚本中并加载它,但我并不喜欢这样做。

It's a good solution. Generally you would want to do this as part of a build script for the site, and throw in a call to a minifier at the same time.

这是一个很好的解决方案。通常,您希望将此作为站点构建脚本的一部分,并同时调用 minifier。

回答by Zekrom_Vale

I would omit type="text/javascript"

我会省略 type="text/javascript"

The "type" attribute is required in HTML 4, but optional in HTML5.

“type”属性在 HTML 4 中是必需的,但在 HTML5 中是可选的

But I still think that merging src is impossible (as of HTML5 and CSS3)

但我仍然认为合并 src 是不可能的(从 HTML5 和 CSS3 开始)

回答by Quick Joe Smith

You could just copy the contents of all the jquery.ui.* files into one file. As an added bonus, your pages would load slightly faster.

您可以将所有 jquery.ui.* 文件的内容复制到一个文件中。作为额外的好处,您的页面加载速度会稍快一些。