Use wildcard in src attribute of <script> tag?

拥有回忆 提交于 2019-12-05 02:45:49
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.

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

Nope, not in the way you're thinking of. You could do something that's similar if you're using JavaScript loaders (e.g. 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.

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. UglifyJS or Closure Compiler) may solve your problem.

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('ajax/test.js', function() {
    alert('Load was performed.');
});
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.

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.

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.

I would omit type="text/javascript"

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!