volume one
volume one

Reputation: 7563

jQuery CDN Fallback Doesn't Work

I am using the solution from the second answer to this question Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail

The solution says to use the following code to provide a fallback to a local copy of jQuery in case Google's CDN doesn't work properly:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>if (!window.jQuery) { document.write('<script src="/path/jquery.1.11.1.min.js"><\/script>'); }
</script>

I've implemented this code and run it on my local machine which has IIS installed. It works fine as long as I have an internet connection. If I disable my internet connection, it's not falling back to my local copy of jQuery and the web application breaks (i.e. loss of functionality).

Is there a new way to doing fallbacks or was my testing method flawed?

Upvotes: 2

Views: 581

Answers (1)

Arkantos
Arkantos

Reputation: 6608

You can even create a script element and add it your head or body at the top like this

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

         <script>
            if(!window.jQuery){
              var script = document.createElement('script');
              script.src = 'jquery-1.11.2.min.js';
              document.head.appendChild(script);
            }    
         </script>

I tried it both offline and online and it works :)

Upvotes: 2

Related Questions