本文介绍了你从哪里包含 jQuery 库?谷歌JSAPI?CDN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几种方法可以包含 jQuery 和 jQuery UI,我想知道人们在使用什么?

There are a few ways to include jQuery and jQuery UI and I'm wondering what people are using?

  • 谷歌JSAPI
  • jQuery 的网站
  • 您自己的站点/服务器
  • 另一个 CDN

我最近一直在使用 Google JSAPI,但是发现设置 SSL 连接需要很长时间,甚至只能解析 google.com.我一直在为 Google 使用以下内容:

I have recently been using Google JSAPI, but have found that it takes a long time to setup an SSL connection or even only to resolve google.com. I have been using the following for Google:

<script src="https://www.google.com/jsapi"></script>
<script>
google.load('jquery', '1.3.1');
</script>

我喜欢使用 Google 的想法,以便在访问其他网站时缓存它并节省我们服务器的带宽,但如果它一直是网站的缓慢部分,我可能会更改包含.

I like the idea of using Google so it's cached when visiting other sites and to save bandwidth from our server, but if it keeps being the slow portion of the site, I may change the include.

你用什么?你有什么问题吗?

What do you use? Have you had any issues?

刚刚访问了 jQuery 的网站,他们使用以下方法:

Just visited jQuery's site and they use the following method:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

Edit2:以下是我去年在没有任何问题的情况下包含 jQuery 的方式:

Here's how I've been including jQuery without any problems for the last year:

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

区别在于去除了http:.通过删除它,您无需担心在 http 和 https 之间切换.

The difference is the removal of http:. By removing this, you don't need to worry about switching between http and https.

推荐答案

毫无疑问,我选择让 JQuery 由 Google API 服务器提供服务.我没有使用 jsapi 方法,因为我没有利用任何其他 Google API,但是如果它发生变化,那么我会考虑它...

Without a doubt I choose to have JQuery served by Google API servers. I didn't go with the jsapi method since I don't leverage any other Google API's, however if that ever changed then I would consider it...

第一:Google api 服务器分布在世界各地,而不是我的单个服务器位置:更近的服务器通常意味着访问者的响应时间更快.

First: The Google api servers are distributed across the world instead of my single server location: Closer servers usually means faster response times for the visitor.

第二:许多人选择在 Google 上托管 JQuery,因此当访问者访问我的网站时,他们的本地缓存中可能已经有了 JQuery 脚本.预先缓存的内容通常意味着访问者的加载时间更快.

Second: Many people choose to have JQuery hosted on Google, so when a visitor comes to my site they may already have the JQuery script in their local cache. Pre-cached content usually means faster load times for the visitor.

第三:我的网络托管公司向我收取使用的带宽费用.如果访问者可以在其他地方获得相同的文件,那么每个用户会话消耗 18k 是没有意义的.

Third: My web hosting company charges me for the bandwidth used. No sense consuming 18k per user session if the visitor can get the same file elsewhere.

我了解我对 Google 提供正确的脚本文件以及在线和可用的部分信任.到目前为止,我还没有对使用 Google 感到失望,并且会继续这种配置,直到它变得有意义为止.

I understand that I place a portion of trust on Google to serve the correct script file, and to be online and available. Up to this point I haven't been disappointed with using Google and will continue this configuration until it makes sense not to.

值得指出的一件事...如果您的网站上同时存在安全和不安全页面,您可能需要动态更改 Google 源代码,以避免在加载不安全页面时出现通常的警告安全页面中的内容:

One thing worth pointing out... If you have a mixture of secure and insecure pages on your site you might want to dynamically change the Google source to avoid the usual warning you see when loading insecure content in a secure page:

这是我想出的:

<script type="text/javascript">
    document.write([
        "<script src='",
        ("https:" == document.location.protocol) ? "https://" : "http://",
        "ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'></script>"
    ].join(''));
</script>

更新 9/8/2010 -一些建议通过删除 HTTP 和 HTTPS 并简单地使用以下语法来降低代码的复杂性:

UPDATE 9/8/2010 -Some suggestions have been made to reduce the complexity of the code by removing the HTTP and HTTPS and simply use the following syntax:

<script type="text/javascript">
    document.write("<script src='//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'></script>");
</script>

此外,如果您想确保加载了最新的主要版本的 jQuery 库,您还可以更改 url 以反映 jQuery 主要版本:

In addition you could also change the url to reflect the jQuery major number if you wanted to make sure that the latest Major version of the jQuery libraries were loaded:

<script type="text/javascript">
    document.write("<script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>");
</script>

最后,如果您不想使用 Google 并且更喜欢 jQuery,您可以使用以下源路径(请记住,jQuery 不支持 SSL 连接):

Finally, if you don't want to use Google and would prefer jQuery you could use the following source path (keep in mind that jQuery doesn't support SSL connections):

<script type="text/javascript">
    document.write("<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>");
</script>

这篇关于你从哪里包含 jQuery 库?谷歌JSAPI?CDN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 20:26