什么是加载jQuery的最佳方法是什么? [英] What's the best way to load jQuery?

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

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/547384/where-do-you-include-the-jquery-library-from-google-jsapi-cdn\">Where你包括jQuery库?谷歌JSAPI? CDN?

我有使用jQuery的应用程序。该应用程序可以使用jQuery的几乎所有的最新版本如1.7或1.8。

I have an application that uses jQuery. The application could use almost any recent version of jQuery such as 1.7 or 1.8.

谁能给我一个建议,我怎么能code我的应用程序,以最大限度地发挥它的发现最近的jQuery的版本能够满足我的用户需要缓存的机会。例如,我应该尝试寻找来自谷歌的CDN一个版本和我应该尝试寻找这种情况下,哪个版本?

Can anyone give me a suggestion as to how I could code my application to maximize the chances of it finding a recent version of jQuery that would meet my needs in the users cache. For example should I try and look for a version from the google CDN and in which case which version should I try and look for?

推荐答案

有关公共网站使用内容分发网络(CDN)是相当普遍。要引用一个你包括他们像任何其他本地文件的脚本标记:

The use of a Content Delivery Network (CDN) for public web sites is quite common. To reference one you include them with a script tag like any other local file:

jQuery的例子

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

谷歌的例子

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

微软例如

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js" type="text/javascript"></script>

我应该始终引用最新版本?

这样做时,例如使用jQuery CDN时总是指向最新的版本存在的风险。

Should I always reference the latest version?

There is a risk when doing so, when for example using the jQuery CDN to always point to the latest version.

在过去的几个版本的几种方法已经去precated,即:切换() - 鼠标事件,生活() 死()等。

In the last few versions several methods have been deprecated, i.e: toggle() - for mouse events, live(), die() and others.

据我从jQuery论坛上知道的是,那些去precated方法将在1.9版本中永久删除。

As far as I know from the jQuery forums is that those deprecated methods will be permanently removed in version 1.9.

在jQuery的除了计划推出1.9和2.0并拢。然而2.0不是1.9的延续,而是将在沿侧开发。

In addition jQuery plans to release 1.9 and 2.0 close together. However 2.0 is not a continuation of 1.9 but instead will be developed on along-side.

2.0将不再支持IE6,IE7或IE8。 jQuery的1.9将留在所有previous浏览器版本兼容。

2.0 will not support IE6, IE7 or IE8. jQuery 1.9 will stay compatible with all previous browser versions.

阅读它<一个href=\"http://blog.jquery.com/2012/07/01/jquery-1-9-and-2-0-tldr-edition/\">here.

由于这些原因,我不会建议自动总是指向最新版本,但明确引用您正在支持特定版本。

For those reasons I would not recommend to automatically always point to the latest version but explicitly reference the specific version you are supporting.

这很少发生的这可能发生的CDN已关闭。为了以防万一,所以你不必承担后果,你可以实现一个备用计划。

It rarely happens but it could happen that the CDN is down. Just in case, so you don't have to suffer the consequence, you can implement a fallback plan.

// Check if jQuery was initialized and if not (CDN was down for example), then
// load jQuery from a local source.
if(typeof jQuery === 'undefined'){
    document.write(unescape("%3Cscript src='yourlocalpath/jquery.1.x.min.js' type='text/javascript'%3E%3C/script%3E"));
}

CDN可用性和性能评价

关于对现有的CDN的质量和性能,我碰到这个很有意思的上royal.pingdom.com。文章

来自Pingdom的报告显示,最常用的,和jQuery的免费主机;谷歌,微软和媒体寺;已经证明可靠的,但其性能不一致。

The report from Pingdom revealed that the most commonly used, and free hosts of jQuery; Google, Microsoft and Media Temple; have proved reliable but with inconsistent performance.

要评估网络,Pingdom的来自欧洲各地和北美的多个位置,每分钟执行一次测试,全天候30天。

To evaluate the networks, Pingdom performed tests from multiple locations across Europe and North America, once per minute, around the clock for 30 days.

结果发现,所有三个提供了出色的可用性,但是,这不是对性能的情况下。

The results found that all three offered excellent availability but that wasn't the case for performance.

对于不使用HTTPS或安全服务器上的站点,媒体寺是迄今为止速度最快其次是谷歌在欧洲,但在北美落后。对于HTTPS网站谷歌是最快的欧洲与谷歌和微软进行北美相似。

For sites that don't use HTTPS or secure servers, Media Temple was by far the fastest followed by Google in Europe but lagged behind in North America. For HTTPS sites Google was the fastest in Europe with Google and Microsoft performing similar in North America.

微软执行欧洲最差的,但即使与谷歌在北美的整体。

Microsoft performed the worst in Europe but was even with Google in North America overall.

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

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