CDN with failover

Loading your scripts from a CDN is generally a good idea, for many reasons. Using a CDN helps load your site faster, possibly use less bandwidth, and reduces costs.

Once in awhile, your CDN may be unavailable, and you want the site to work anyway. For example, if you have an internal intranet application, but your office external internet is down. You can try to use the CDN by default, but if it doesn’t work, you’ll load the script file from your server.

Sample Code

<script type="text/javascript">
    function addScriptToDOM(url) {
        document.write('\x3Cscript type="text/javascript" src="' + url + '">\x3C/script>');
    }
</script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">window.jQuery || addScriptToDOM("/Scripts/jquery-1.11.3.min.js")</script>