简体   繁体   English

多个Javascript文件,但只有一个HTTP请求

[英]Multiple Javascript files but only one HTTP request

I want to separate my JS out nicely so each object is in its own file, I'll probably structure my directories and files like: 我想很好地分离我的JS,所以每个对象都在自己的文件中,我可能会构建我的目录和文件,如:

/js/modules/module1.js /js/pages/page1.js /js/modules/module1.js /js/pages/page1.js

Problem is I don't want to have like 50 HTTP requests for each file, is there a way to do this? 问题是我不希望每个文件都有50个HTTP请求,有没有办法做到这一点?

This technique is quite basic and logical. 这种技术非常基础和合乎逻辑。 The situation is that we have several calls to external Javascript files within our HTML's header, which means several HTTP requests from the client's browser. 情况是我们在HTML的标题中有几个外部Javascript文件调用,这意味着来自客户端浏览器的几个HTTP请求。 How do we approach reducing all these HTTP requests? 我们如何减少所有这些HTTP请求?

Combine them so that there is at least only one call to an external Javascript file, Avoid breaking any Javascript-dependent functionality on the web page, Minify the Javascript to save on bandwidth 将它们组合起来,以便至少只有一次调用外部Javascript文件,避免破坏网页上任何依赖于Javascript的功能,缩小Javascript以节省带宽

http://falcon1986.wordpress.com/2010/01/11/combine-and-minify-javascript-reduce-number-of-http-requests/ http://falcon1986.wordpress.com/2010/01/11/combine-and-minify-javascript-reduce-number-of-http-requests/

Two ways came to my mind: 我想到了两种方式:

  1. Put only one js link in your site, like /scripts/all.js , and on the request for that js, compile and merge all files and serve them. 在您的站点中只放置一个js链接,例如/scripts/all.js ,并在请求该js时,编译并合并所有文件并提供它们。
  2. Use JavaScript dependency managers, to load scripts on-demand (rather than all of them, even if not used) 使用JavaScript依赖关系管理器,按需加载脚本(而不是所有这些,即使没有使用)

You can use a PHP file to serve all of them combined. 您可以使用PHP文件来组合所有这些文件。 This technique is used by many PHP softwares such as IP.board 许多PHP软件(如IP.board)都使用此技术

In case of ASP.NET, this will help - http://abhijitjana.net/2011/10/06/bundling-and-minification-in-asp-net-4-5/ 在ASP.NET的情况下,这将有所帮助 - http://abhijitjana.net/2011/10/06/bundling-and-minification-in-asp-net-4-5/

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

相关问题 如何将多个javascript文件的请求合并到一个http请求中? - How to combine requests for multiple javascript files into one http request? 用Javascript处理多个JS文件的请求 - Javascript one request for multiple JS files 多个 HTML 文件仅链接到一个 Javascript 文件 - Multiple HTML files linked to only one Javascript file 使用一个http请求下载文件,不进行压缩 - Download files with one http request and no zipping AngularJS通过组合javascript文件来减少HTTP请求 - AngularJS reducing HTTP request by combining javascript files 是否可以只发送一个请求来获取多个外部javascript文件? - Is it possible to send only one request to fetch more than One external javascript files? javascript:在不同的 iframe 中加载相同的 url,如何只使用一个 http 请求? - javascript: Loading the same url in different iframes, how to use only one http request? 多个JavaScript文件合并为一个 - Multiple JavaScript files, combine into one Ajax请求正确循环,但没有发送到服务器以获取更多的多个文件。 仅在阵列中保存一个文件 - Ajax request looping correctly but not sending to the server for more multiple files. Only saves one file in the array 加载外部javascript,但是针对一个文件javascript反复进行HTTP请求 - Load External javascript, but HTTP request repeatedly for one file javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM