PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » jQuery » Fallback for CDN hosted jQuery

Fallback for CDN hosted jQuery

Several big companies offer copies of jQuery hosted on their CDN's (Content Delivery Network). Most notoriously Google, but also Microsoft and jQuery themselves. A lot of people swear by this since it saves bandwidth, downloads faster, and perhaps even stays cached jumping between different sites that use the same script.

There is always that twinge of doubt though, that perhaps something goes wrong with these big companies CDN at the script isn't available (it happens). It's more reliable to use your own website, as hey, if they are loading your webpage, then your server is up and will server the script just fine, albeit with out the benefits of the CDN.

So perhaps the best solution is to use both methods! Use the CDN first, and if it fails, load the local copy. Here is a technique:

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='/js/jquery-1.4.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>

Reference URL

Subscribe to The Thread

  1. Mateus says:

    Man, you’ve read my thoughts, I was searching for this today!

    Thanks!

  2. CSB says:

    You can never be too safe :)

  3. Very cool! I think all sites must use this “solution”.

  4. Parvez says:

    Nice solution man! I was searching for this :)

  5. traq says:

    huh… I always had a bit of worry in the back of my head about a “hotlinked” js file becoming unavailable for some reason. Hosting my own scripts always seemed safer. Good solution.

  6. You have a special tendency to release important articles at just the right time! Thanks.

  7. CIDIC says:

    How would you set it up to have more than one backup? If you wanted 3 or 4 would you just use more if statements? I would think there is a more efficient way to check until one that works is found.

    • TeMc says:

      I think more than 1 backup (where the “1″ is the local copy you have) doesn’t make sense.

      Having to wait for the client to make multiple connections and fail is way slower than just tring to load from 1 CDN and fallback to local.

  8. Maybe it’s because i’m at the other side of the ocean but i’ve seen it happen that cdn’s load slowly. How long does it take for the response to time out en switch to local? Should I force a time-out after .. miliseconds. How does that work?

    On a sidenote, why not simply:
    !window.jQuery && document.write('')

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~