Let a big company host your JavaScript libraries
Big companies often use
Content Delivery Networks (CDNs). Such a network consists of servers spread all over the world that all host the same content which ensures fast delivery. Some CDNs also host popular JavaScript libraries for the public, for free. Dave Ward gives
three reasons why you might want to use such hosted libraries instead of storing them next to your own code:
- Decreased latency: Chances are high that a CDN server is closer to a user than your own server. Thus: faster delivery.
- Increased parallelism: Some browsers impose per-host limits on the number of connections. If your JavaScript libraries come from a different server, more of your own content can be loaded in parallel.
- Better caching: If several sites use the same CDN, a copy of your library might already be cached in a user’s browser.
Comments:
- Naturally, a reason against it is that it does not always work when you work offline, with file URLs. If that matters, you can dynamically load your libraries and switch to locally stored versions if the URL protocol is file.
- The post suggests to use protocol-relative URLs if you want your site to work both with HTTP and HTTPS. This breaks down if you are using file URLs to test your site (as the CDNs cannot be reached via that protocol).
- The CDN approach is also useful if you just want to quickly try out a small example, without downloading a library.
- Privacy is an issue. If you use Google’s libraries, they will be able to track who comes to your website and from where. [Source: comment from Wesley P]
Here are links to what is available:
- Google (most major JavaScript libraries)
- Microsoft hosts several jQuery files, including jQuery Templates.
- Note: ajax.microsoft.com was renamed to ajax.aspnetcdn.com.
Isn’t the latter more ugly? Or was the intention to subtly market ASP.NET?
- cdnjs hosts less popular libraries.
Be careful about what code you link to! Only do it if the owner of a site expressly allows it; this can cost them a lot of money.