Load jQuery from Google with Fallback to Local File

Quick one today, because I have shit to do…

If you don’t know why you should load your jQuery file from Google read about some of the advantages here.

But what if for some reason Google’s CDN is unavailable…like say you’re working on the site from a local copy and U haz no interwebs???

Write a simple script that checks for jQuery in the namespace, if it doesn’t find it, load a local copy:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>window.jQuery || document.write("<script src='javascripts/jquery.min.js'>x3C/script>")</script>

That’s it. It tries to load google’s, if “jQuery” is undefined, include the local version.

Leave a Reply

Your email address will not be published. Required fields are marked *