jQuery通过Google CDN最佳做法 [英] jQuery via Google CDN best practices

查看:126
本文介绍了jQuery通过Google CDN最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过Google的CDN加载jQuery.

I'm loading jQuery via Google's CDN using the following code.

我的主要问题是,如果用户访问我的网站并且尚未预先缓存jQuery,将会发生什么情况.他会下载Google版本和我自己的版本吗?并发在这里如何工作?

My main question is what will happen if a user hits my site and hasn't yet got jQuery pre-cached. Will he download the Google version and my own? How does concurrency here work?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    if(typeof jQuery == 'undefined') {
        //<![CDATA[
        document.write("<script src='/includes/jquery-1.4.2.min.js' type='text/javascript'><\/script>");
        //]]>
    }
</script>

谢谢.

推荐答案

在您的示例代码中,他们将下载google版本的如果因为其他站点而没有该版本.然后如果出于某种原因Google关闭,他们将下载您的版本,而不会同时下载两者.仅当 第一个(来自Google)失败时,才请求第二个.

In your example code they will download the google version if they don't already have it because of another site. Then if for some reason google is down, they'll download your version, they won't download both. The second is only requested if the first (from Google) fails.

检查如下:

  1. 我们是否缓存了Google版本?
    • -好的,可以使用它.
    • -从Google下载并使用.
  1. Do we have the google version cached?
    • Yes - Ok good to go, use it.
    • No - Download it from Google, use it.
  • -确定加载正常,if()为false,继续.
  • -糟糕! Google加载失败,无论是从缓存还是提取中,都需要从其他位置加载
    • 通过刚刚添加的新<script>标签从您的网站加载它.
    • Yes - ok it loaded fine, the if() is false, continue on.
    • No - oh snap! Google load failed, either from cache or the fetch, need to load it from elsewhere
      • Load it from your site via a new <script> tag just added.

      这篇关于jQuery通过Google CDN最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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