Where Should You Source jQuery Libraries From?
When including jQuery and jQuery UI in your projects, there are several options available. Let's delve into the advantages and disadvantages of each method.
Google JSAPI vs. CDN
The Google JSAPI provides a convenient way to access jQuery from Google's distributed servers. This can improve load times and potentially reduce bandwidth consumption. However, setting up an SSL connection or resolving google.com may introduce delays.
jQuery's Site or Your Server
Hosting jQuery on your own server or from jQuery's site allows for complete control over the version and potential customization. However, this approach requires additional setup and maintenance, and potential issues with bandwidth usage on your server.
Other CDNs
Using a third-party CDN, such as ajax.googleapis.com, can provide the benefits of a distributed network without having to maintain your own infrastructure. The downside is that you rely on the CDN's reliability and availability.
Google JSAPI Recommended
Many developers recommend using Google's JSAPI for distributing jQuery. The distributed servers reduce latency, and the caching mechanisms prevent multiple downloads of the library. Additionally, other Google services may leverage this approach, streamlining code inclusion.
Dynamic Source Switching for Secure Protocols
If you have secure and insecure pages on your site, you can dynamically switch the Google source to avoid mixed content warnings:
<code class="html"><script type="text/javascript"> document.write([ "\<script src='", ("https:" == document.location.protocol) ? "https://" : "http://", "ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'\>\<\/script\>" ].join('')); </script></code>
Simplified Dynamic Source
To simplify this code further, you can use the following syntax:
<code class="html"><script type="text/javascript"> document.write("\<script src='//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'\>\<\/script\>"); </script></code>
The above is the detailed content of Where is the best source for jQuery libraries in your web projects?. For more information, please follow other related articles on the PHP Chinese website!