Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load local if CDNJS not available

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!

like image 473
Keenan Payne Avatar asked Nov 17 '13 22:11

Keenan Payne


People also ask

How can we ensure jQuery loads even if the CDN is down?

Note: Use the full path to your downloaded jQuery file. In my case, both HTML and jQuery file is in the same folder. Now, we will see how to load local jQuery in case our CDN fails. Write the following lines of code, and it will add jQuery from the machine when CDN fails.

Can jQuery be accessed via CDN?

Officially there are two ways of using jQuery: CDN Based Version - You can include jQuery library into your HTML code directly from Content Delivery Network (CDN). Local Installation - You can download jQuery library on your local machine and include it in your HTML code.

Why use jQuery?

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

What is CDN JavaScript library?

CDNs are used widely for delivering stylesheets and Javascript files (static assets) of libraries like Bootstrap, jQuery etc. Using CDN for those library files is preferable for a number of reasons: Serving libraries' static assets over CDN lowers the request burden on an organization's own servers.


1 Answers

<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>
like image 196
adam187 Avatar answered Oct 09 '22 05:10

adam187