Use local files if CDN is Offline

9

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

5

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

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

<head>
    <!-- ... -->
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>

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

<script>
    if ( typeof $ === "undefined" ) {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "//servidor.com.br/jquery-1.11.3.min.js";
        document.body.appendChild(s);
    }
</script>

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