开发者

What are the advantages in including Jquery library from google's ajax api library? [duplicate]

开发者 https://www.devze.com 2023-04-13 09:45 出处:网络
This question already has answers he开发者_开发百科re: Closed 11 years ago. Possible Duplicate: Why should I use Google's CDN for jQuery?
This question already has answers he开发者_开发百科re: Closed 11 years ago.

Possible Duplicate:

Why should I use Google's CDN for jQuery?

I have seen many of the web programmers including Jquery library from google's ajax api library. Is there any real advantage in not hosting those libraries in our server other than saving 'some' space?. Is it a good habit doing so?


5 Reasons Why Businesses Should Use CDN
7 Reasons to use a Content Delivery Network

CDN being Content Delivery Network (hosted libraries)

Though you should always have a local cache in case a remote store fails. (The HTML5 Boilerplate demonstrates this very well):

<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if necessary -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.5.1.min.js">\x3C/script>')</script>


There are a few advantages to letting Google host the API for you.

  1. Decreased Latency
  2. Increased Parallelism
  3. Better Caching

Decreased Latency

A CDN — short for Content Delivery Network — distributes your static content across servers in various, diverse physical locations. When a user’s browser resolves the URL for these files, their download will automatically target the closest available server in the network.

In the case of Google’s AJAX Libraries CDN, what this means is that any users not physically near your server will be able to download jQuery faster than if you force them to download it from your arbitrarily located server.

There are a handful of CDN services comparable to Google’s, but it’s hard to beat the price of free! This benefit alone could decide the issue, but there’s even more.

Increased Parallelism

To avoid needlessly overloading servers, browsers limit the number of connections that can be made simultaneously. Depending on which browser, this limit may be as low as two connections per hostname.

Using the Google AJAX Libraries CDN eliminates one request to your site, allowing more of your local content to downloaded in parallel. It doesn’t make a gigantic difference for users with a six concurrent connection browser, but for those still running a browser that only allows two, the difference is noticeable.

Better Caching

Potentially the greatest benefit of using the Google AJAX Libraries CDN is that your users may not need to download jQuery at all.

No matter how well optimized your site is, if you’re hosting jQuery locally then your users must download it at least once. Each of your users probably already has dozens of identical copies of jQuery in their browser’s cache, but those copies of jQuery are ignored when they visit your site.

However, when a browser sees references to CDN-hosted copies of jQuery, it understands that all of those references do refer to the exact same file. With all of these CDN references point to exactly the same URLs, the browser can trust that those files truly are identical and won't waste time re-requesting the file if it's already cached. Thus, the browser is able to use a single copy that's cached on-disk, regardless of which site the CDN references appear on.

This creates a potent "cross-site caching" effect which all sites using the CDN benefit from. Since Google's CDN serves the file with headers that attempt to cache the file for up to one year, this effect truly has amazing potential. With many thousands of the most trafficked sites on the Internet already using the Google CDN to serve jQuery, it's quite possible that many of your users will never make a single HTTP request for jQuery when they visit sites using the CDN.

Even if someone visits hundreds of sites using the same Google hosted version of jQuery, they will only need download it once!


Pro:

  • You save the traffic. jQuery and co. are able to take a lot of space on the traffic.

  • Google and co. CDNs are created to do this job. If someone is able to offer such content very fast than networks like this.

  • For a long time browsers are not able to load more than two Javascript files from one domain. If you use jQuery from google.com your Browser will be able to load other application specific files at the same time. If this is important today is the question

  • Many people use Google or Microsoft services or visit websites which are linked by their jQuery Version. That means, you have the big chance, that your browser already cached jQuery somewhere – shorter waiting times while loading!

Contra:

  • You cant use it if you dont have internet connection (local lan only applications)


  • There would be a much better chance that a user visiting your site would already have Google's version cached, instead of your own hosted version. (faster, potentially)

  • You don't have to host it

  • You don't need to deal with caching it (well unless your using a fallback approach and Google's version is down)

  • If a user is in Europe and your server is in Fargo they would most likely pull Google's version down from a much closer server (faster, potentially)

  • Saves your bandwidth (every bit counts when you're paying)

I'd recommend taking a look at the Html5Boilerplate, they've got a good fallback approach implemented for when you can't reach Google's CDN.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号