Last Updated: February 25, 2016
·
4.481K
· g

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');

            });

        }
    }
]);

2 Responses
Add your response

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 ·