简体   繁体   English

包含我的外部javascripts的最佳/最快方式是什么? 我使用jQuery

[英]What is the best/fastest way to include my external javascripts? I use jQuery

I am nearly done with my site and am optimising it at the moment; 我差不多完成了我的网站,目前正在优化它; I would like to know the best and fastest way to include all my external javascript files. 我想知道包含所有外部JavaScript文件的最佳和最快的方法。 I want the site to download as quick as possible, but it has quite a few (10 or so) external javascript files that need to be loaded; 我希望网站尽快下载,但它有很多(10个左右)需要加载的外部JavaScript文件; some are jQuery library files from Google's AJAX API and some are mine. 一些是来自谷歌AJAX API的jQuery库文件,有些是我的。

I'm sure I read a while ago that I could call all external scripts using a bit of javascript code, in effect, only calling one external file from the browsers point of view. 我确定我刚才读过,我可以使用一些javascript代码调用所有外部脚本,实际上,只从浏览器的角度调用一个外部文件。

Do you see what I mean? 你明白我的意思吗?

Many thanks 非常感谢

  1. Combine all your Javascript into one external file (you can do this dynamically and save the cached result); 将所有Javascript组合到一个外部文件中(您可以动态执行此操作并保存缓存结果);
  2. Minify that file; 缩小该文件;
  3. Version that file (I use the mtime of a preconfigured file for this); 该文件的版本(我使用预配置文件的mtime);
  4. Gzip the file if the client supports that; 如果客户端支持,请对文件进行Gzip; and
  5. Use a far futures Expires header on the file. 在文件上使用远期期货Expires标头。

What you're referring to (using Google's AJAX Libraries service) is another way to handle this that falls under the heading of a CDN (Content Delivery Network). 您所指的是(使用Google的AJAX Libraries服务)是处理此问题的另一种方式,属于CDN(内容交付网络)的标题。 The idea being that the file is stored in multiple plallllces and the client will download the closest (and that result will be saved). 这个想法是文件存储在多个plallllces中,客户端将下载最接近的文件(并且将保存该结果)。

That's hard or just awkward to combine with other techniques and I've found that doing multiple external loads this way completely erodes any perceived benefit (unless that's your only external load) so I use the method listed above instead. 与其他技术结合起来很困难或者很尴尬,我发现以这种方式进行多次外部加载会完全侵蚀任何感知的好处(除非那是你唯一的外部负载),所以我使用上面列出的方法。

My guess is to combine the library files to just one file (except the ones hosted at Google). 我的猜测是将库文件合并到一个文件(谷歌托管的文件除外)。 Each call to your server takes quite some resources, so you're better off with just one call. 每次拨打您的服务器都需要相当多的资源,所以只需拨打一个电话就可以获得更好的服务。 You can even combine the files online: 您甚至可以在线组合文件:

http://yui.2clics.net/ http://yui.2clics.net/

我建议将所有脚本合并到一个JS文件中,然后使用YUI Compressor将它们打包成一个较小的文件。

要使用脚本浏览器必须下载它们,所以我认为没有任何区别你如何称呼它们...除非你在服务器上使用压缩并在客户端解压缩。

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

相关问题 在页面中包含交互式外部内容的最佳方法(JavaScript,AJAX,jQuery) - Best way to include interactive external content in a page (JavaScript, AJAX, jQuery) 在(webpack和vue2)中包含外部js文件的最佳方法是什么 - What is best way to include external js files in (webpack and vue2) 多次使用jQuery选择器的最有效/最快的方法是什么? - What is the most efficient/fastest way to use a jQuery selector multiple times? 在带有phonegap的jQuery mobile中链接页面/加载javascript的最佳实践是什么? - What is the best practice in linking pages/ loading javascripts in jquery mobile with phonegap? 存储要与jquery一起使用的数据的最佳方法是什么? - What is the best way to store data to use with jquery? 检查网站是否在线的最佳和最快方法是什么 - What is the best and fastest way to check if the site is online 包含库的最佳方式是什么? - what is the best way to include libraries? 包含Javascript的最佳方法是什么? - What is the best way to include Javascript? 在div上对div排序的最佳方式(最快)是什么? (在没有JQuery的javascript中) - what's the best way (fastest) to sort divs on a div ? (in javascript without JQuery) 将外部JavaScript调用纳入网站的最佳方法 - Best way for include external javascript call into website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM