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
449 views
in Technique[技术] by (71.8m points)

javascript - 你在哪里包含jQuery库?谷歌JSAPI? CDN?(Where do you include the jQuery library from? Google JSAPI? CDN?)

There are a few ways to include jQuery and jQuery UI and I'm wondering what people are using?(有几种方法可以包含jQuery和jQuery UI,我想知道人们在使用什么?)

Google JSAPI(谷歌JSAPI) jQuery's site(jQuery的网站) your own site/server(你自己的网站/服务器) another CDN(另一个CDN) I have recently been using Google JSAPI, but have found that it takes a long time to setup an SSL connection or even only to resolve google.com.(我最近一直在使用Google JSAPI,但发现设置SSL连接需要很长时间,甚至只能解决google.com问题。) I have been using the following for Google:(我一直在谷歌使用以下内容:) <script src="https://www.google.com/jsapi"></script> <script> google.load('jquery', '1.3.1'); </script> I like the idea of using Google so it's cached when visiting other sites and to save bandwidth from our server, but if it keeps being the slow portion of the site, I may change the include.(我喜欢使用Google的想法,因此它在访问其他网站时被缓存并从我们的服务器节省带宽,但如果它一直是网站的缓慢部分,我可能会更改包含。) What do you use?(你用什么?) Have you had any issues?(你有什么问题吗?) Edit: Just visited jQuery's site and they use the following method:(编辑:刚刚访问过jQuery的网站,他们使用以下方法:) <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> Edit2: Here's how I've been including jQuery without any problems for the last year:(编辑2:这是我去年包括jQuery没有任何问题的方式:) <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> The difference is the removal of http: .(不同的是删除http: .) By removing this, you don't need to worry about switching between http and https.(通过删除它,您不必担心在http和https之间切换。)   ask by Darryl Hein translate from so

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

1 Reply

0 votes
by (71.8m points)

Without a doubt I choose to have JQuery served by Google API servers.(毫无疑问,我选择让Google API服务器提供JQuery服务。)

I didn't go with the jsapi method since I don't leverage any other Google API's, however if that ever changed then I would consider it...(我没有使用jsapi方法,因为我没有利用任何其他Google API,但如果改变了那么我会考虑它...) First: The Google api servers are distributed across the world instead of my single server location: Closer servers usually means faster response times for the visitor.(第一: Google api服务器分布在世界各地而不是我的单一服务器位置:更紧密的服务器通常意味着访问者的响应时间更快。) Second: Many people choose to have JQuery hosted on Google, so when a visitor comes to my site they may already have the JQuery script in their local cache.(第二:许多人选择在Google上托管JQuery,因此当访问者访问我的网站时,他们可能已在其本地缓存中安装了JQuery脚本。) Pre-cached content usually means faster load times for the visitor.(预缓存内容通常意味着访问者的加载时间更短。) Third: My web hosting company charges me for the bandwidth used.(第三:我的网络托管公司向我收取使用的带宽费用。) No sense consuming 18k per user session if the visitor can get the same file elsewhere.(如果访问者可以在其他地方获得相同的文件,则每个用户会话消耗18k是没有意义) I understand that I place a portion of trust on Google to serve the correct script file, and to be online and available.(我了解到,我将一部分信任放在Google上,以提供正确的脚本文件,并在线提供。) Up to this point I haven't been disappointed with using Google and will continue this configuration until it makes sense not to.(到目前为止,我对使用谷歌并没有感到失望,并将继续这种配置,直到它有意义为止。) One thing worth pointing out... If you have a mixture of secure and insecure pages on your site you might want to dynamically change the Google source to avoid the usual warning you see when loading insecure content in a secure page:(有一点值得指出......如果您的网站上有安全页面和不安全页面的混合,您可能需要动态更改Google源代码,以避免在安全页面中加载不安全内容时看到的通常警告:) Here's what I came up with:(这是我想出的:) <script type="text/javascript"> document.write([ "<script src='", ("https:" == document.location.protocol) ? "https://" : "http://", "ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'></script>" ].join('')); </script> UPDATE 9/8/2010 - Some suggestions have been made to reduce the complexity of the code by removing the HTTP and HTTPS and simply use the following syntax:(更新20109月8日 - 通过删除HTTP和HTTPS并简单地使用以下语法,已经提出了一些建议来降低代码的复杂性:) <script type="text/javascript"> document.write("<script src='//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'></script>"); </script> In addition you could also change the url to reflect the jQuery major number if you wanted to make sure that the latest Major version of the jQuery libraries were loaded:(此外,如果您想确保加载了最新的主要版本的jQuery库,您还可以更改URL以反映jQuery主编号:) <script type="text/javascript"> document.write("<script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>"); </script> Finally, if you don't want to use Google and would prefer jQuery you could use the following source path (keep in mind that jQuery doesn't support SSL connections):(最后,如果您不想使用Google并且更喜欢使用jQuery,则可以使用以下源路径(请记住,jQuery不支持SSL连接):) <script type="text/javascript"> document.write("<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>"); </script>

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

...