最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Multiple sources for a javascript file - Stack Overflow

programmeradmin6浏览0评论

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

<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

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

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>

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

<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

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

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>
Share asked Jul 15, 2012 at 16:24 Amanjot SinghAmanjot Singh 1011 gold badge3 silver badges12 bronze badges 2
  • 2 It is not at all clear what you're asking for. A single <script> tag can import one script from one URL. – Pointy Commented Jul 15, 2012 at 16:25
  • 1 you cannot CSV list files to load, each script must have a src to a single file – freefaller Commented Jul 15, 2012 at 16:26
Add a ment  | 

5 Answers 5

Reset to default 2

The example you posted looks exactly like the query string interface for the minify PHP library: http://github./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 path is the path from your web root that you want it to look for scripts under.

I've used it before and I've found it quite effective. It concatenates, minifies, caches, and serves JS and 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.

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

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

<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>

I think you'll get what you need from dynamically loading external javascript files:

http://ntt/2008/02/10/4-ways-to-dynamically-load-external-javascriptwith-source.html

http://www.javascriptkit./javatutors/loadjavascriptcss.shtml

The second line you posted requests scripts from the server dynamically. the b parameter of the request tells it you want scripts and the f parameter 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.

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>

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 "pressed" (minified). Some builders:

  • Google Closure Compiler (I like and use this since 2009).
  • YUI Compressor
发布评论

评论列表(0)

  1. 暂无评论