你在哪里包含jQuery库?谷歌JSAPI? CDN? [英] Where do you include the jQuery library from? Google JSAPI? CDN?

查看:146
本文介绍了你在哪里包含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?


  • Google JSAPI

  • jQuery的网站

  • 您自己的网站/服务器

  • 另一个CDN

  • Google JSAPI
  • jQuery's site
  • your own site/server
  • another CDN

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

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>

编辑2:以下是我如何包含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上以提供正确的脚本文件,并在线和可用。到目前为止,我对使用谷歌并没有感到失望,并且会继续这种配置,直到有意义为止。

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>

更新2010年9月8日 -
有些建议有通过删除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主编号,如果您想确保加载了最新的主要版本的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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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