14

I have a couple of JavaScript files that I use on every single project and currently use CDNJS to load them.

However, I was trying to see if there was a good way to check if the CDN is available, or if the files are available from the CDN. Then of course, if the files are not available on the CDN, I would load them locally.

Here are the JS files I currently use:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>window.jQuery || document.write('<script type="text/javascript" src="./scripts/jquery.min.js">\x3C/script>')</script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/retina.js/1.0.1/retina.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.3.0/respond.js"></script>

<!--[if (gte IE 6)&(lte IE 8)]>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/selectivizr/1.0.2/selectivizr-min.js"></script>
<![endif]-->

As you can see, if I have a fallback for loading jQuery, but not for any of the other files.

Any help would be greatly appreciated!

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
Keenan Payne
  • 796
  • 2
  • 15
  • 32
  • Check [How to load local script files as fallback in cases where CDN are blocked/unavailable?](http://stackoverflow.com/questions/5257923/how-to-load-local-script-files-as-fallback-in-cases-where-cdn-are-blocked-unavai/5531821#5531821) But you've already using preferred solution. – Anto Jurković Nov 17 '13 at 23:13
  • Why not do the same thing ? – The Alpha Nov 17 '13 at 23:24
  • I don't really know how to... Where I can check for `window.jQuery` and `window.Modernizr` - it doesn't seem like retina.js and respond.js have a function I can check for. – Keenan Payne Nov 17 '13 at 23:27
  • 1
    It seems that retina.js has `root.Retina` and `Retina`. And respond.js has `respond`. – Anto Jurković Nov 17 '13 at 23:41
  • Thank you very much @AntoJurkovic. I'm just not the best at reading JavaScript so I couldn't really figure it out. That helped me a lot. – Keenan Payne Nov 17 '13 at 23:50
  • So, your question is what object/variable to check to see if CDN library is available or not? I am not sure if somewhere exists such list. – Anto Jurković Nov 17 '13 at 23:57

1 Answers1

12
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/retina.js/1.0.1/retina.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.3.0/respond.js"></script>
<script>
window.jQuery || document.write('<script type="text/javascript" src="./scripts/jquery.min.js">\x3C/script>')
window.Modernizr || document.write('<script type="text/javascript" src="./scripts/modernizr.min.js">\x3C/script>')
window.RetinaImage || document.write('<script type="text/javascript" src="./scripts/retina.js">\x3C/script>')
window.respond || document.write('<script type="text/javascript" src="./scripts/respond.js">\x3C/script>')
</script>
adam187
  • 3,193
  • 21
  • 15