简体   繁体   English

在src属性中使用通配符<script> tag?

[英]Use wildcard in src attribute of <script> tag?

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? 或者我们只是忍受它?

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. 我建议使用Google的闭包编译器将所有类似的(如果不是全部)javascript文件合并到一个文件中。 It will be slightly large, but would cut down on http request. 它会略大,但会减少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. 我一般不愿意提供添加新的javascript库来解决太多javascript库的问题:)此外,这似乎是更直接的解决方案。 Current we use the Google closure API to compress and merge all our javascript and CSS and build time with ANT. 目前我们使用Google封闭API来压缩和合并我们所有的javascript和CSS,并使用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 这也可以在某种程度上直接使用apache2虚拟主机/ htaccess(参见html5boilerplate.com)获取示例和限制

Nope, not in the way you're thinking of. 不,不是你想的那样。 You could do something that's similar if you're using JavaScript loaders (eg RequireJS or 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 (eg UglifyJS or Closure Compiler ) may solve your problem. 如果您将文件合并到一个文件中的问题是努力的话,那么JS minifiers(例如UglifyJSClosure Compiler )可以解决您的问题。

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 getScript for each item. 实际上,正如其他人可能已经提到的那样,您可以在其中添加自己的脚本文件,并执行类似添加数组路径的操作,循环调用每个项目的getScript

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

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并将动态捆绑所有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。

I would omit type="text/javascript" 我会省略type="text/javascript"

The "type" attribute is required in HTML 4, but optional in HTML5 . HTML 4中需要“type”属性, 但HTML5中是可选的

But I still think that merging src is impossible (as of HTML5 and CSS3) 但我仍然认为合并src是不可能的(从HTML5和CSS3开始)

You could just copy the contents of all the jquery.ui.* files into one file. 您可以将所有jquery.ui。*文件的内容复制到一个文件中。 As an added bonus, your pages would load slightly faster. 作为额外的奖励,您的页面加载速度会稍快一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM