Javascript 加载 jQuery 的最佳方式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12332697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:44:34  来源:igfitidea点击:

What's the best way to load jQuery?

javascriptjqueryasp.netasp.net-mvcgoogle-cdn

提问by Angela

Possible Duplicate:
Where do you include the jQuery library from? Google JSAPI? CDN?

可能的重复:
您从何处包含 jQuery 库?谷歌JSAPI?CDN?

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 的应用程序。该应用程序几乎可以使用任何最新版本的 jQuery,例如 1.7 或 1.8。

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?

任何人都可以就我如何编写应用程序以最大限度地提高它在用户缓存中找到满足我的需求的最新版本的 jQuery 的机会给我一个建议。例如,我应该尝试从 google CDN 中寻找一个版本,在这种情况下我应该尝试寻找哪个版本?

回答by Nope

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:

将内容交付网络 (CDN) 用于公共网站是很常见的。要引用一个,您可以像任何其他本地文件一样将它们包含在脚本标记中:

jQuery example

jQuery 示例

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

Google example

谷歌示例

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

Microsoft example

微软示例

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

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.

这样做是有风险的,例如,当使用 jQuery CDN 始终指向最新版本时。

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

在过去的几个版本的几种方法已经过时,即:toggle()-鼠标事件,live()die()和其他人。

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 版中永久删除。

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.

此外,jQuery 计划同时发布 1.9 和 2.0。然而,2.0 不是 1.9 的延续,而是会同时开发。

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

2.0 将不支持 IE6、IE7 或 IE8。jQuery 1.9 将与所有以前的浏览器版本保持兼容。

Read about it 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.

出于这些原因,我不建议自动始终指向最新版本,而是明确引用您支持的特定版本。

What if the CDN is down

如果 CDN 关闭怎么办

It rarely happens butit 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.

这种情况很少发生,可能会发生 CDN 关闭的情况。为了以防万一,因此您不必承担后果,您可以实施后备计划。

// 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 availability and performance evaluation

CDN 可用性和性能评估

Regarding the quality and performance on available CDNs I came across this very interesting articleon royal.pingdom.com.

关于可用 CDN 的质量和性能,我在 Royal.pingdom.com 上看到了这篇非常有趣的文章

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 的报告显示,jQuery 最常用和免费的主机;谷歌、微软和 Media Temple;已被证明是可靠的,但性能不一致。

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

为了评估网络,Pingdom 在欧洲和北美的多个地点进行了测试,每分钟一次,持续 30 天。

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

结果发现,所有三个都提供了出色的可用性,但性能并非如此。

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.

对于不使用 HTTPS 或安全服务器的网站,Media Temple 是迄今为止最快的,其次是谷歌在欧洲,但在北美落后。对于 HTTPS 站点,谷歌是欧洲最快的,谷歌和微软在北美的表现相似。

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

微软在欧洲的表现最差,但在北美的整体表现与谷歌不相上下。

回答by jfriend00

Pick the most recent version of jQuery that meets your needs. At this moment, that would be 1.8. Then link to it on a free and widely used CDN (Google or Microsoft probably).

选择满足您需要的最新版本的 jQuery。此时,这将是 1.8。然后在免费且广泛使用的 CDN(可能是 Google 或 Microsoft)上链接到它。

You should link to a specific version of jQuery. Though there is a link for the latest version of jQuery, you should NOT link to that because when that changes, your site could break without you knowing it. You should link only to a specific version of jQuery. Here's the code for linking to 1.8.1 on the Google CDN.

您应该链接到特定版本的 jQuery。尽管有最新版本的 jQuery 的链接,但您不应链接到该链接,因为当它发生变化时,您的网站可能会在您不知情的情况下中断。您应该只链接到特定版本的 jQuery。这是在 Google CDN 上链接到 1.8.1 的代码。

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

If you really want to optimize your browser cache hits, you would pick and test with a version of jQuery that is widely deployed now (I believe there are web statistics somewhere on how much each version is being used) and then you would regularly update that to track the most used version. I suspect that this is micro-optimizing and just picking any recent version that you've tested with is suitable and will give you the benefits of using a common CDN.

如果你真的想优化你的浏览器缓存命中率,你会选择和测试一个现在广泛部署的 jQuery 版本(我相信有关于每个版本使用多少的网络统计数据),然后你会定期更新它跟踪最常用的版本。我怀疑这是微优化,只需选择您测试过的任何最新版本都是合适的,并且会给您带来使用通用 CDN 的好处。

回答by Joey Gennari

I would recommend referencing a static version number from the Googleor MicrosoftCDN. Use the latest version for development now. If you make changes that require newer features, just change your reference in your layouts.

我建议参考来自GoogleMicrosoftCDN的静态版本号。立即使用最新版本进行开发。如果您进行需要更新功能的更改,只需更改布局中的参考即可。

The Google CDN looks like:

谷歌 CDN 看起来像:

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

回答by Robert Greiner

For the latest version, you can load directly from jQuery:

对于最新版本,您可以直接从 jQuery 加载:

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

There are also other Content Delivery Networksthat you can load from as well.

您也可以从其他内容交付网络加载。

Some of the popular CDNs are:

一些流行的 CDN 是:

  • jQuery (code.jquery.com)
  • Microsoft (ajax.microsoft.com)
  • Google (ajax.googleapis.com)
  • jQuery (code.jquery.com)
  • 微软 (ajax.microsoft.com)
  • 谷歌 (ajax.googleapis.com)