简体   繁体   English

是否可以只发送一个请求来获取多个外部javascript文件?

[英]Is it possible to send only one request to fetch more than One external javascript files?

I am working on a webpage ,on which I need to fetch around 10 external .js files.So browser makes 10 requests to fetch them. 我正在一个网页上工作,我需要在其上获取大约10个外部.js文件。所以浏览器发出10个请求来获取它们。 Is there any way which I can bring all of them in one shot?? 有什么方法可以一次性带来所有这些吗?

Is there any way I come bring all of them in One shot?? 我有什么方法可以一次性带来所有这些?

No, but you can bundle and compress them all into one single JS file. 不,但您可以将它们捆绑并压缩成一个单独的JS文件。

That would save you the total round-trips time consumption for the all 10 files. 这样可以节省所有10个文件的总往返时间消耗。

You can use online tools to achieve it easily, like: http://jscompress.com/ 您可以使用在线工具轻松实现,例如: http//jscompress.com/

First of all - if browser gets all 10 JS files, they will be stored in cache, so next request will load files from cache. 首先 - 如果浏览器获取所有10个JS文件,它们将存储在缓存中,因此下一个请求将从缓存中加载文件。

About merging files into one - you can create some script which will fetch all 10 files, merge their content and save in a specified file. 关于将文件合并为一个 - 您可以创建一些脚本来获取所有10个文件,合并其内容并保存在指定的文件中。 Or do it by your own if JS files are static. 或者如果JS文件是静态的,请自己动手。

You can try these 你可以尝试这些

  • compress and minify your javascript files 压缩和缩小您的JavaScript文件
  • Import all js files into one manually and do compress 手动将所有js文件导入一个并进行压缩

Comments and whitespace are not needed for javascript execution; javascript执行不需要注释和空格; Removing them will speed up script execution times 删除它们将加快脚本执行时间

OR 要么

If possible, you can execute the script asynchronously. 如果可能,您可以异步执行脚本。 HTML5 has got this feature. HTML5已经具备此功能。 <script> tag has got async boolean property. <script>标签具有async布尔属性。 make use of it. 利用它。

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

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