Yepnope load from CDN with Fallback
YepNope
http://yepnopejs.com/
Modernizr
http://modernizr.com/
If you are using Modernizr and/or Yepnope, you can use this method to load your favorite library from the CDN and fall-back to your local version if the initial load fails.
//Load
yepnope([
{
load: [
'//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js',
'//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js',
'//cdnjs.cloudflare.com/ajax/libs/raphael/1.5.2/raphael-min.js'
],
complete: function(){
if ( !window.jQuery ) {
console.log('CDN Failed - Loading local version of jQuery.');
yepnope('assets/js/libs/jquery-1.7.2-min.js');
};
if ( !window.swfobject ) {
console.log('CDN Failed - Loading local version of SWFObject.');
yepnope('assets/js/libs/swfobject-2.2-min.js');
};
if ( !window.Raphael ) {
console.log('CDN Failed - Loading local version of Raphael.');
yepnope('assets/js/libs/raphael-1.5.2-min.js');
};
}
},
{
load: [
'assets/js/file-one.js',
'assets/js/file-two.js'
],
complete: function(){
//Loaded
console.log('Primary JS Loaded');
//Ready
$(function(){
console.log('DOM Ready');
});
}
}
]);Written by Greg Babula
Related protips
2 Responses
Check this out: https://github.com/sgarbesi/fallback.js
It's a library that I wrote that allows you to add as many fallback CDN's as you want.
over 1 year ago
·
This is awesome, thank you :)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#