问题描述
尝试加载 Google托管的jQuery (或其他Google托管的库),但是如果Google尝试失败,是否加载我的jQuery副本?
What would be a good way to attempt to load the hosted jQuery at Google (or other Google hosted libs), but load my copy of jQuery if the Google attempt fails?
我并不是说Google很脆弱.在某些情况下,Google副本被阻止(例如,显然在伊朗).
I'm not saying Google is flaky. There are cases where the Google copy is blocked (apparently in Iran, for instance).
我会设置一个计时器并检查jQuery对象吗?
Would I set up a timer and check for the jQuery object?
两个副本都通过的危险是什么?
What would be the danger of both copies coming through?
并没有真正寻找诸如仅使用Google一个"或仅使用您自己的一个"之类的答案.我理解那些论点.我也了解到,该用户很可能已缓存了Google版本.我通常在考虑云的后备.
Not really looking for answers like "just use the Google one" or "just use your own." I understand those arguments. I also understand that the user is likely to have the Google version cached. I'm thinking about fallbacks for the cloud in general.
此部分已添加...
由于Google建议使用google.load加载ajax库,并在完成后执行回调,所以我想知道这是否是序列化此问题的关键.
Since Google suggests using google.load to load the ajax libraries, and it performs a callback when done, I'm wondering if that's the key to serializing this problem.
我知道这听起来有点疯狂.我只是想弄清楚它是否可以可靠地完成.
I know it sounds a bit crazy. I'm just trying to figure out if it can be done in a reliable way or not.
更新:jQuery现在托管在Microsoft的CDN上.
Update: jQuery now hosted on Microsoft's CDN.
推荐答案
您可以这样实现:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="/path/to/your/jquery"><\/script>');
</script>
这应该在页面的<head>
中,并且所有jQuery ready事件处理程序都应该在<body>
中,以避免发生错误(尽管它不是万无一失的!).
This should be in your page's <head>
and any jQuery ready event handlers should be in the <body>
to avoid errors (although it's not fool-proof!).
不不使用Google托管的jQuery的另一个原因是,在某些国家/地区,Google的域名被禁止.
One more reason to not use Google-hosted jQuery is that in some countries, Google's domain name is banned.
这篇关于使用Google托管jQuery的最佳方法,但在Google上使用我的托管库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!