Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

jquery - 使用Google托管jQuery的最佳方法,但在Google上使用我的托管库失败(Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail)

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托管库)上加载托管jQuery ,但如果Google尝试失败则加载我的jQuery副本的好方法?)

I'm not saying Google is flaky.

(我并不是说Google很脆弱。)

There are cases where the Google copy is blocked (apparently in Iran, for instance).

(在某些情况下,Google副本被阻止(例如,显然在伊朗)。)

Would I set up a timer and check for the jQuery object?

(我会设置一个计时器并检查jQuery对象吗?)

What would be the danger of both copies coming through?

(两份复印件通过的危险是什么?)

Not really looking for answers like "just use the Google one" or "just use your own."

(并非真正在寻找诸如“仅使用Google一个”或“仅使用您自己的一个”之类的答案。)

I understand those arguments.

(我理解那些论点。)

I also understand that the user is likely to have the Google version cached.

(我也了解用户很可能已经缓存了Google版本。)

I'm thinking about fallbacks for the cloud in general.

(我通常在考虑云的回退。)


Edit: This part added...

(编辑:这部分添加...)

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.

(由于Google建议使用google.load加载ajax库,并且完成后会执行回调,所以我想知道这是否是序列化此问题的关键。)

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.

(我只是想弄清楚它是否可以可靠地完成。)


Update: jQuery now hosted on Microsoft's CDN.

(更新:jQuery现在托管在Microsoft的CDN上。)

http://www.asp.net/ajax/cdn/

(http://www.asp.net/ajax/cdn/)

  ask by Nosredna translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can achieve it like this:

(您可以这样实现:)

<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>

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!).

(这应该在页面的<head>并且所有jQuery ready事件处理程序都应该在<body>以避免发生错误(尽管它不是万无一失的!)。)

One more reason to not use Google-hosted jQuery is that in some countries, Google's domain name is banned.

(使用Google托管的jQuery的另一个原因是,在某些国家/地区,Google的域名被禁止。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...