如果CDNJS不可用,则加载本地 [英] Load local if CDNJS not available
本文介绍了如果CDNJS不可用,则加载本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在每个项目中都有几个JavaScript文件,目前正在使用CDNJS加载它们.
I have a couple of JavaScript files that I use on every single project and currently use CDNJS to load them.
但是,我试图查看是否有一种很好的方法来检查CDN是否可用,或者是否可以从CDN中找到文件.然后,当然,如果CDN上没有文件,我将在本地加载它们.
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.
以下是我当前使用的JS文件:
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]-->
如您所见,如果我有一个用于加载jQuery的备用,但没有其他任何文件的备用.
As you can see, if I have a fallback for loading jQuery, but not for any of the other files.
任何帮助将不胜感激!
推荐答案
<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>
这篇关于如果CDNJS不可用,则加载本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文