从谷歌jQuery的加载或在本地,如果不在线 [英] Loading jQuery from Google or locally if not online
问题描述
现在,我已经在我的code以下链接:
Right now I have the following links in my code:
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
等等......
etc ...
我想利用google.com缓存副本。我听到谷歌是最好的来源,但请纠正我,如果我错了。
I'd like to make use of the google.com cached copies. I heard google is the best source but please correct me if I am wrong.
反正是有可能对我来说,code我的应用程序,以便它使用code从谷歌(如果可用)和当地如果没有。仅供参考我使用微软MVC3和Msoft云服务器。
Anyway is it possible for me to code my application so it uses code from google if available and locally if not. FYI I am using Microsoft MVC3 and the Msoft cloud servers.
感谢
推荐答案
当然,看看他们是如何做到这一点在 HTML5样板。
Sure, check out how they do it in HTML5 boilerplate.
如果你看一看index.html文件底部> GitHub的回购之内,你会看到下面的...
If you take a look at the bottom of the index.html file within the GitHub repo, you'll see the following...
<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/X.X.X/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="local/jquery-X.X.X.min.js">\x3C/script>')</script>
的注:在code片段上方的 XXX 应与你使用jQuery版本号来代替(如 1.8.2 ) 的
NB: In the code snippet above X.X.X should be replaced with the jQuery version number that you're using (e.g. 1.8.2).
- 首先,试图抢CDN版本(谷歌的CDN网址上面使用,当然你可以链接到任何你喜欢的来源)。
- 紧接着,我们检查
的jQuery
全局对象。 - 如果
的jQuery
不存在,明显的假设是,我们没能得到来自CDN的code,所以后来我们文件撰写
script标签从本地源,而不是得到一份拷贝。
- First, an attempt is made to grab the CDN version (Google's CDN url is used above, but of course you could link to any source you like).
- Immediately afterwards, we check for the
jQuery
global object. - If
jQuery
does not exist, the obvious assumption is that we didn't manage to get the code from the CDN, so then wedocument.write
a script tag to get a copy from a local source instead.
这篇关于从谷歌jQuery的加载或在本地,如果不在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!