33

There are many posts about how to fallback a JQuery CDN to a local copy by checking the existance of the global variable JQuery.

My question is , how to do the same to twitter-bootstrap ? Is there a variable defined in bootstrap so that I can check to make sure the CDN is available ?

BTW, I use netdna.bootstrapcdn.com as my bootstrap CDN

Community
  • 1
  • 1
John Wang
  • 4,562
  • 9
  • 37
  • 54

2 Answers2

40

This is what I did and it works fine

<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
<script>    $.fn.modal || document.write('<script src="Script/bootstrap.min.js">\x3C/script>')</script>
Marcel
  • 401
  • 1
  • 3
  • 3
  • 1
    Nice reading from Hanselman: http://www.hanselman.com/blog/CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.aspx – Clarice Bouwer Nov 20 '13 at 13:34
20

I didn't see a specific variable that twitter-bootstrap exposes for this purpose and they don't attach all of their plugins to a namespace a la jQuery UI. Your next best bet is to test for one of the bootstrap plugins. Perhaps something like this:

if(typeof($.fn.modal) === 'undefined') {
    //load bootstrap locally
}

The unfortunate thing about this is that it is brittle. If the modal plugin is ever renamed or removed this check would always fail.

Bill Hayden
  • 1,086
  • 7
  • 6