JQuery through Google CDN Best Practices

I download jQuery via Google CDN using the following code.

My main question is what happens if a user clicks on my site and has not yet received jQuery pre-cached. Will he download the version of Google and my own? How does concurrency 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> 

Thanks.

+7
jquery cdn
source share
1 answer

In your sample code, they will download the version of Google if they do not already have it because of another site. Then, if for some reason Google does not work, they will download your version, they will not download both. The second request is only requested if the first fails (from Google).

Verification is performed as follows:

  • Do we have a google cache version?
    • Yes - It's good to go, use it.
    • No. Download it from Google, use it.
  • Is jQuery defined (JavaScript object)?
    • Yes - fine, it loaded fine, if() - false, continue.
    • No - oh snap! Error loading Google, either from the cache or extract, you must download it from another place
      • Download it from your site using the newly added <script> .
+9
source share

All Articles