Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created February 1, 2011 00:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/805146 to your computer and use it in GitHub Desktop.
Save cowboy/805146 to your computer and use it in GitHub Desktop.
Google CDN -> jQuery CDN fallback
<!--
See JSFiddle:
http://jsfiddle.net/cowboy/6asBw/
-->
<!-- unminified -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
// If jQuery failed to load from the Google AJAX APIs CDN...
window.jQuery || (function(doc, script, matches){
// Get the preceding script tag.
script = doc.scripts;
script = script[ script.length - 2 ];
// Parse the requested jQuery version number and whether or not it was minified.
matches = script.src.match(/\/(\d+\.\d+)(\.0)?(\.\d+)?\/jquery(\.min)?\.js$/);
// If there were matches, load the jQuery CDN version of the specified script.
// Note that Google CDN version 1.4.0 maps to jQuery CDN version 1.4 and that
// the jQuery CDN doesn't handle 1 or 1.4 the same way to Google CDN does, so be
// sure to specify the full x.y.z version!
matches && doc.write('<script src="http://code.jquery.com/jquery-' + matches[1] + ( matches[3] || '' ) + (matches[4] || '') + '.js"><\/script>');
})(document);
</script>
<!-- minified -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>/*cowboy-jquery-cdn-fallback*/window.jQuery||(function(c,a,b){a=c.scripts;a=a[a.length-2];b=a.src.match(/\/(\d+\.\d+)(\.0)?(\.\d+)?\/jquery(\.min)?\.js$/);b&&c.write('<script src="http://code.jquery.com/jquery-'+b[1]+(b[3]||"")+(b[4]||"")+'.js"><\/script>')})(document)</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment