Using CDN for jQuery is a very good practice and has lots of benefits over using jQuery from local.
Using CDN for jQuery – Google
1 |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
Using CDN for jQuery – Microsoft
1 |
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> |
Benefits of CDN over local
1. Faster Access
Using CDN means the jQuery files will be accessed faster over the internet from CDN in comparision to the file that you may use from your server.
2. Use of Cached File
Since many other application already use jQuery files from CDN, they will get downloaded in the client’s browser cache. So when the client accesses your application, it will use the jQuery that already cached.
This will increase faster downloading of application.
3. Location Specific access
Since Google and Microsoft have their data centers all across the globe, the jQuery file will get downloaded in faster time as it gets downloaded from the closet datacenter.
e.g. If your application is hosted in North America and a user from Africa is trying to access the application, then if Jquery was to be downloaded from the server, it would have taken longer time.
But using CDN for jQuery, the file gets downloaded from the nearest available datacenter. This results in faster downloads.
4. Ease of using latest jQuery file.
Although not recommended. Please read full details here on jQuery Blog for not using jquery-latest-js.
What if the CDN server is offline?
If the CDN is offline or you are not able to connect to internet for local development then you can always go back to the local copy
Please see the code below
1 2 3 4 5 |
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> <script> // load jQuery from a local path if the CDN is unavailable (window.jQuery || document.write('<script src="/scripts/jquery-1.9.0.min.js"><\/script>')); </script> |
Using minified version
For more faster downloaded from CDN you can use the minified version of jQuery files
1 |
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script> |
Thank you for reading this article.