2

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?

Community
  • 1
  • 1
volume one
  • 6,800
  • 13
  • 67
  • 146
  • is it not injecting the fallback script at all? if you add 'debugger;' after the window.jQuery check does it hit? – atmd Jan 27 '15 at 16:45
  • 1
    Is your local copy of jQuery the same version as the CDN copy? What's the error in the console? If jQuery is not loaded it should be something along the lines of `$ is undefined`. – Rory McCrossan Jan 27 '15 at 16:45
  • 1
    Are you sure your local path is correct? – j08691 Jan 27 '15 at 16:45
  • My local copy is the same version. Except that the name of my jQuery file is `jquery.1.11.1.min.js` rather than just `jquery.min.js` – volume one Jan 27 '15 at 16:49
  • @A.Wolff no, I'm using 1.11.1. I just updated my question. – volume one Jan 27 '15 at 16:50
  • Don't you have error in network tab? Is your local server running? – A. Wolff Jan 27 '15 at 16:52
  • @atmd do you mean like this? `` I tried running that but I don't see anything happening in the firebug console – volume one Jan 27 '15 at 17:03
  • that would suggest that window.jQuery isnt null, how about does that give you anything? – atmd Jan 27 '15 at 17:04
  • @A.Wolff If I click on the NET tab, it says `URL: GET jquery.min.js STATUS: Aborted DOMAIN: ajax.googleapis.com` – volume one Jan 27 '15 at 17:05
  • @RoryMcCrossan I can't see any errors in my console tab relating to `$` or `jquery` – volume one Jan 27 '15 at 17:07
  • @volumeone But this is relevant to CDN link, not local copy of jQuery. Regarding your issue, are you calling it from head section of page or at least before the document is fully rendered? – A. Wolff Jan 27 '15 at 17:07
  • I'm calling it at the end of my page. not in the head. if i remove the whole fallback code and just replace it with my local copy then it works fine too. – volume one Jan 27 '15 at 18:04

1 Answers1

2

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 :)

Arkantos
  • 6,530
  • 2
  • 16
  • 36