jQuery 我应该在生产代码中使用 Google 的 JSAPI 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1106755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I use Google's JSAPI in production code?
提问by Frank Krueger
Possible duplicate of:
可能重复:
should-i-link-to-google-apis-cloud-for-js-libraries
should-i-link-to-google-apis-cloud-for-js-libraries
also many other discussions, including:
还有许多其他讨论,包括:
Where do you include the jQuery library from? Google JSAPI? CDN?Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail
你从哪里包含 jQuery 库?谷歌JSAPI?CDN?使用 Google 托管的 jQuery 的最佳方法,但退回到我在 Google 上的托管库失败
I was looking at the Tiny MCE plugin example and saw this code in the head
of the document:
我正在查看 Tiny MCE 插件示例,并head
在文档中看到了此代码:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3");
</script>
I've never seen this way to load jQuery.
我从未见过这种加载 jQuery 的方式。
- Is this recommended for production?
- What is the benefit of this method?
- 这是否推荐用于生产?
- 这种方法有什么好处?
回答by Keith Bentrup
Yes, definitely. Google encourages it. Everyone benefits. It's more likely to be in their cache, and it's one less file that you have to serve.
当然是。谷歌鼓励它。每个人都受益。它更有可能在他们的缓存中,而且您必须提供的文件少了一个。
回答by Nosredna
As others have pointed out answering similar questions, there's a downside. In some countries (such as Iran), these are apparently blocked, breaking the website.
正如其他人指出的那样回答类似的问题,有一个缺点。在一些国家(如伊朗),这些显然被封锁,破坏了网站。
回答by Fusspawn
The benefit is it's hosted on googles super low latency and fast servers. you can also just use
好处是它托管在谷歌的超低延迟和快速服务器上。你也可以只使用
<script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script>
its the same effect.
它的效果是一样的。
回答by Amir Arad
keep in mind that google jsapi loads the scripts only after the document itself is loaded.
请记住,google jsapi 仅在加载文档本身后加载脚本。
So, if (for example) you are using jquery's $(document).ready()
in your web app, you'll have to switch to google.setOnLoadCallback()
.
因此,如果(例如)您$(document).ready()
在 Web 应用程序中使用 jquery ,则必须切换到google.setOnLoadCallback()
.
回答by IEnumerator
I believe that the Google JSAPI is also asynchronous and helps avoid the "toll booth" best described by "Imagine there's a 4-lane highway between your web browser and the internet itself. This highway is optimize to let pictures, text, and css fly by. But, when it comes to external scripts, the highway creates a toll booth that slows traffic. The worst part is that pictures text, and css caught behind these scripts have to wait until they pass through" - Andres Vidal
我相信谷歌 JSAPI 也是异步的,有助于避免“收费站”最好描述为“想象一下你的网络浏览器和互联网本身之间有一条 4 车道的高速公路。这条高速公路经过优化,可以让图片、文本和 css 飞起来通过。但是,当涉及到外部脚本时,高速公路会创建一个收费站,导致交通变慢。最糟糕的部分是图片文本,这些脚本后面的 css 必须等到它们通过”- Andres Vidal
The toll-booth is critical and must be avoided at all times.
收费站很重要,必须始终避开。
回答by Cesar Hermosillo
I think this method will help you a lot for the following reasons:
我认为这种方法会对你有很大帮助,原因如下:
Google uses a Content Delivery Network and that will make that the users that are far away from your location can download your jquery libraries faster than if they did that from your site.
Google 使用内容交付网络,这将使远离您所在位置的用户可以比从您的站点下载您的 jquery 库更快。
Also it will reduces the request to your server and will make first time users to download jquery javascript from google's server, and if the user has been in another similar site with this kind of implementation he won't need to download it again.
此外,它会减少对您的服务器的请求,并使第一次用户从谷歌的服务器下载 jquery javascript,如果用户已经在另一个类似的站点中使用这种实现,他将不需要再次下载它。
So I think that this will help you app/site
所以我认为这会帮助你应用程序/网站
回答by Muhammad Soliman
this file is after compression is 24KB, Addition of such file will increase HTTP requests and waiting for the response and execution and parse time that browser will take... if you say the file itself is cached everywhere, even if the file is cached in the browser, don't forget to consider the time it takes to read from disk, execute and parse...
此文件压缩后为 24KB,添加此类文件将增加 HTTP 请求并等待浏览器将花费的响应和执行和解析时间...如果您说文件本身缓存在任何地方,即使文件缓存在浏览器,不要忘记考虑从磁盘读取、执行和解析所需的时间......
all of this for only getting the jQuery file or other common JS, I think referring directly to the requested resource is better
所有这些只是为了获取 jQuery 文件或其他常见的 JS,我认为直接引用请求的资源更好
check Google's best practices for more info.
查看 Google 的最佳实践以了解更多信息。
回答by Jon
China has 500 million internet users and is not the only country that blocks google apis, this makes any website that uses http://www.google.com/jsapidysfunctional. There is a small upside: due to the asynchronous load technique, these sites don′t display the same hang waiting to load as other sites that use the direct reference as eg:
china拥有 5 亿互联网用户,并不是唯一一个屏蔽 google api 的国家,这使得任何使用http://www.google.com/jsapi 的网站都无法 正常工作。有一个小的好处:由于异步加载技术,这些站点不会像其他使用直接引用的站点那样显示等待加载的挂起,例如: