Use local files if CDN is Offline


I would like to know if it is possible to perform a callback if a CDN is offline the system uses the local files.

Ex: I use the Fontawesome CDN but if the user runs out of the internet or the CDN goes offline, it is possible to create a callback for the system to use the local files.

Obs. The system should give preference to the use of CDN if it is online and the user has internet.

asked by anonymous 11.05.2015 / 16:48

1 answer


Yes. I'll give you an example with jQuery loaded into Google's CDN.

In%% of your HTML, you should add the CDN script:

    <!-- ... -->
    <script src="//"></script>

In head , add a code to check that body has been set, and if not, add the "fallback" script to load:

    if ( typeof $ === "undefined" ) {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "//";

Just remember that if you have scripts that require jQuery, they should be loaded dynamically after jQuery is loaded, following the same logic as above.

11.05.2015 / 17:30