Just this morning the internet connection got all flaky on me and loading jQuery from Google’s CDN started taking a long time. Because of this, things like my menu, a slider, some graphing applications and a few other things started taking FOREVER to load. Chrome kept telling me it was waiting on the https address of jQuery. This made me think. “What if, 5 years down the road, jQuery 1.8.1 just isn’t available anymore?” So, I decided I better make sure there’s some redundancy in my scripts.
So, if you need to load jQuery, or anything really, and you prefer to use a central repository, but you want to have a local backup, just in case, do this…
<!-- Load jQuery from Google CDN --> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <!-- Load jQuery from a local copy if loading from Google fails --> <script type="text/javascript">// <![CDATA[ window.jQuery || document.write('<script type="text/javascript" src="local/path/to/jquery-1.8.1.min.js"></script>') // ]]>
Oh, and if you need to load jQuery UI too, it’s a little different.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script> <!-- If juery-ui didn't load from Google, load it locally --> <script type="text/javascript">// <![CDATA[ if (typeof jQuery.ui == 'undefined') { document.write('<script src="scripts/jquery-ui-1.8.23.min.js"></script>'); } // ]]>
These examples obviously use specific versions of the scripts. If you are (very likely) wanting to use different versions, just substitute them. Duh, right?