包括最新版本的jQuery的最佳方法是什么? [英] What is the best way to include latest version of jQuery?

查看:76
本文介绍了包括最新版本的jQuery的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Wordpress开发站点,我想使用最新版本的jQuery.

I'm developing sites using Wordpress and I want to use the lates version of jQuery.

为确保使用最新版本,我从二进制盆景的例子.

To make sure I use the lates version, I have found this piece of code from Binary Bonsai's example.

我看到的是,他实际上是通过Google API链接到jQuery的.

What I see, is that he actually links to jQuery at Google API.

所以我的问题是,什么更好.

So my question is, what is better.

要链接到外部页面上的jQuery吗?
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

To link to jQuery on an external page?
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

还是将jquery文件保存在本地?
wp_enqueue_script('jquery', '/js/jquery-1.4.2.min.js');

or to have the jquery file locally?
wp_enqueue_script('jquery', '/js/jquery-1.4.2.min.js');

更新

感谢Colin,答案实际上在这里:
为什么我应该将Google的CDN用于jQuery?

Thanks to Colin, the answer is actually here:
Why should I use Google's CDN for jQuery?

但是为了防止您像我一样在搜索词中没有Google,我将复制约翰·吉岑(John Gietzen)的答案

But tjust in case you don't have Google in your search term, like me, I'll copy the answer from John Gietzen

  1. 它增加了可用的并行性.
    (大多数浏览器一次只能从任何给定站点下载3或4个文件)

  1. It increases the parallelism available.
    (Most browsers will only download 3 or 4 files at a time from any given site)

这会增加缓存命中的机会.
(随着更多的站点遵循这种做法,更多的用户已经准备好文件.)

It increases the chance that there will be a cache-hit.
(As more sites follow this practice, more users have the file already ready.)

它可确保有效载荷尽可能小.
(Google可以预gzip压缩文件,从而使下载时间非常短.)

It ensures that the payload will be as small as possible.
(Google can pre-gzip-compress the file, making the time-to-download very small.)

它减少了服务器使用的带宽量.
(Google基本上提供免费带宽.)

It reduces the amount of bandwidth used by the server.
(Google is basically offering free bandwidth.)

这可确保用户在地理位置上得到密切的响应.
(Google在世界范围内拥有服务器,从而进一步减少了延迟.)

It ensures that the user will get a geographically close response.
(Google has servers all over the world, further decreasing the latency.)

推荐答案

在链接到Google CDN副本时,您可以省略次要版本或点版本,例如:

You can leave out the minor or dot versions when linking to the google CDN copies, like this:

  • jQuery 1.4.2: http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
  • Latest 1.4.x: http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js
  • Latest 1.x.x: http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

鉴于这种灵活性,我更喜欢使用远程版本,只需更改URL即可快速修复错误,并且文件的加载速度更快(在大多数情况下为),因为它是从另一个域(和(不包括对您的域的并行请求).在此处此处.

Given this flexibility, I much prefer the remote version, it's quick to get bug fixes by just changing the URL and the file's loaded faster (in most cases) because it's pulled from another domain (and not in the count of parallel requests to your domain). There are other related questions on this, here and here.

这篇关于包括最新版本的jQuery的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆