1

I was wondering if I can have something like

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

and in case it is not available ...how can I load something like

<script src="../localjs/jquery.js"></script>

And same with CSS

I am asking because I work with CDN a lot, and sometimes I'm developing in Localhost and I am not able to connect to the internet and some scripts stop working, I was wondering if I can do this so I don't have to worry about changing them again and again between CDN and local files.

JuanBonnett
  • 776
  • 3
  • 8
  • 26
  • possible duplicate of [Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail](http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-go) – Alex Jan 17 '14 at 03:49
  • Thank you, May be I didn't take the time to research more. – JuanBonnett Jan 17 '14 at 03:57

2 Answers2

4

You could do something like below:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../localjs/jquery.js">\x3C/script>')</script>

Further reading: CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.

xdazz
  • 158,678
  • 38
  • 247
  • 274
2

Include the following, right after the opening <body> tag:

<body>
    <script>
    if (!jQuery) 
        document.write('<script src="/local/jquery.js"><\/script>');
    </script>

<!-- ... html ... -->

</body>
Alex
  • 34,899
  • 5
  • 77
  • 90