有一些方法来包括jQuery和jQuery UI,我想知道人们使用什么?

谷歌JSAPI jQuery的网站 您自己的站点/服务器 另一个CDN

我最近一直在使用谷歌JSAPI,但发现它需要很长时间来设置SSL连接,甚至只是解析google.com。我一直在使用以下谷歌:

<script src="https://www.google.com/jsapi"></script>
<script>
google.load('jquery', '1.3.1');
</script>

我喜欢使用谷歌的想法,以便在访问其他网站时缓存它,并从我们的服务器节省带宽,但如果它一直是网站的缓慢部分,我可能会改变包括。

你用什么?你有什么问题吗?

编辑:刚刚访问了jQuery的网站,他们使用以下方法:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

Edit2:下面是我如何在过去的一年里没有任何问题地包含jQuery的:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

区别在于去掉了http:。通过删除这个,您就不需要担心在http和https之间切换了。


当前回答

jQuery 1.3.1 min的大小只有18k。我不认为这是一个太大的打击,要求在初始页面加载。它会在那之后被缓存。因此,我自己主持。

其他回答

您希望在外部服务器上托管的一个原因可能是绕过浏览器对特定服务器并发连接的限制。

但是,考虑到您正在使用的jQuery文件可能不会经常更改,浏览器缓存会起作用,使这一点在大多数情况下变得毫无意义。

把它放在外部服务器上的第二个原因是降低到自己服务器的流量。

但是,考虑到jQuery的大小,它可能只占您流量的一小部分。你可能应该尝试优化你的实际内容。

如果我负责“活动”网站,我最好知道正在发生的一切 进入我的网站。出于这个原因,我自己托管了jquery-min版本,要么在同一个服务器上,要么在静态/外部服务器上,但无论哪种方式,只有我(或我的程序/代理)可以在验证/测试每个更改后更新库

如果您想使用谷歌,直接链接可能响应更快。每个库都列出了直接文件的路径。这是jQuery路径

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>

重读一下你的问题,你使用https有什么原因吗?这是谷歌在示例中列出的脚本标记

<script src="http://www.google.com/jsapi"></script>

我将添加这一点作为本地托管这些文件的理由。

Recently a node in Southern California on TWC has not been able to resolve the ajax.googleapis.com domain (for users with IPv4) only so we are not getting the external files. This has been intermittant up until yesterday (now it is persistant.) Because it was intermittant, I was having tons of problems troubleshooting SaaS user issues. Spent countless hours trying to track why some users were having no issues with the software, and others were tanking. In my usual debugging process I'm not in the habit of asking a user if they have IPv6 turned off.

我无意中发现了这个问题,因为我自己正在使用这个特定的“路由”到文件,而且只使用IPV4。我发现了开发工具告诉我jquery没有加载的问题,然后开始做traceroutes等…找到真正的问题。

在此之后,我很可能永远不会回到外部托管文件,因为:谷歌不必因为这成为一个问题而崩溃,而且……这些节点中的任何一个都可能被DNS劫持,并传递恶意js而不是实际文件。一直认为我是安全的,谷歌域永远不会宕机,现在我知道用户和主机之间的任何节点都可能是故障点。

I host it with my other js files on my own server, and, that's that point, combine and minify them (with django-compresser, here, but that's not the point) to be served as just one js file, with everything the site needs put into it. You'll need to serve your own js files anyway, so I see no reason to not add the extra jquery bytes there too - some more kbs are much more cheaper to transfer, than more requests to be made. You are not dependent to anyone, and as soon as your minified js is cached, you're super fast as well.

在第一次加载时,基于CDN的解决方案可能会胜出,因为您必须从自己的服务器加载额外的jquery千字节(但是,没有额外的请求)。不过,我怀疑这种差异是否明显。然后,在第一次加载清除缓存时,您自己的托管解决方案可能总是更快,因为需要更多的请求(和DNS查找)来获取CDN jquery。

我想知道为什么这一点几乎从未被提及,以及cdn是如何占领世界的:)