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

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

问题描述

可能重复:
您从何处包括jQuery库? Google JSAPI? CDN?

Possible Duplicate:
Where do you include the jQuery library from? Google 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.

任何人都可以给我一个建议,告诉我如何编码我的应用程序,以最大程度地找到在用户缓存中可以满足我需要的jQuery新版本.例如,我应该尝试从Google 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>

Google示例

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

Microsoft示例

Microsoft example

<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.

在最近的几个版本中,不推荐使用几种方法,即:toggle()-用于鼠标事件,live()die()等.

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

据我从jQuery论坛了解,那些不推荐使用的方法将在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将与所有以前的浏览器版本保持兼容.

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

详细了解 此处 .

由于这些原因,我不建议您总是总是指向最新版本,而是明确引用您所支持的特定版本.

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的质量和性能,我遇到了这个非常有趣的 文章 .

Pingdom的报告显示,最常用和免费的jQuery主机. Google,Microsoft和Media Temple;已经证明是可靠的,但性能却不一致.

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或安全服务器的网站,Media Temple是迄今为止欧洲最快的网站,紧随其后的是Google,但在北美却落后于其他网站.对于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.

微软在欧洲表现最差,但在整个北美甚至比Google还要差.

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

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

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