Is it wrong to use two CDNs for a Javascript library?

The client site depended on the jquerytools.org CDN, which is currently not working and therefore its site is corrupted (excluding the competent Javascript degradation). He could host the corresponding files locally, but then he doesn't get the benefits of a CDN. How bad is it to link the supposedly same Javascript file to two independent CDNs in the interest of redundancy?

<script src="http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js"></script>
<script src="http://jquerytools.flowplayer.netdna-cdn.com/1.2.6/all/jquery.tools.min.js"></script>

      

+3


source to share


3 answers


You should check if the first CDN actually delivered jQuery before loading the second script:



<script src="http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js"></script>
<script type="text/javascript">
if (typeof jQuery.tools === 'undefined') {
    document.write('<script src="http://jquerytools.flowplayer.netdna-cdn.com/1.2.6/all/jquery.tools.min.js"><\/script>');
}
</script>

      

+5


source


You can do this quite easily and safely using the http://html5boilerplate.com method :

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')</script>

      

What are basically tests for the jQuery object and if it doesn't exist it tries to include it from an alternative source. Just change the url and test object to alternative CDNs and check out the jQuery tools library.



This answer: Testing if jQueryTools loaded should help you test the jQuery tools, but to save the click, the following (untested) should do it:

<script type="text/javascript" src="http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js"></script>
<script>window.jQuery.fn.tabs || document.write('<script src="http://jquerytools.flowplayer.netdna-cdn.com/1.2.6/all/jquery.tools.min.js"><\/script>')</script>

      

+3


source


I would not download both of them. For jquery, we do the following:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="/static/js/jquery-1.7.1.min.js"%3E%3C/script%3E'))</script>

      

To only download the local version if the CDN version didn't load. I'm not sure if this is the best practice, but it works for us. Of course, you need to find a way to determine if jquery-tools have been loaded. If it's initialized later (DomReady) or something like that, it won't work.

+2


source







All Articles