我应该在生产代码中使用 Google 的 JSAPI 吗?

发布于 2024-07-26 03:12:09 字数 921 浏览 9 评论 0原文

可能重复:

应该-i-link-to-google-apis-cloud-for-js-libraries

还有许多其他讨论,包括:

您从哪里包含 jQuery 库? 谷歌JSAPI? CDN? 使用 Google 托管 jQuery 的最佳方式,但回退到我在 Google 上的托管库失败


我正在查看 Tiny MCE 插件示例,并在文档的 head 中看到了以下代码

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("jquery", "1.3");
</script>

:从来没有见过这种加载 jQuery 的方式。

  • 是否建议用于生产?
  • 这种方法有什么好处呢?

Possible duplicate of:

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


I was looking at the Tiny MCE plugin example and saw this code in the head of the document:

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

  • Is this recommended for production?
  • What is the benefit of this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

哭了丶谁疼 2024-08-02 03:12:09

当然是。 Google 鼓励这样做。 每个人都受益。 它更有可能位于他们的缓存中,并且您必须提供的文件少了一个。

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.

挥剑断情 2024-08-02 03:12:09

正如其他人在回答类似问题时指出的那样,有一个缺点。 在某些国家(例如伊朗),这些内容显然被屏蔽,从而破坏了网站。

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.

你对谁都笑 2024-08-02 03:12:09

好处是它托管在谷歌超低延迟和快速的服务器上。
你也可以只使用

<script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script>

它相同的效果。

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.

像你 2024-08-02 03:12:09

请记住,google jsapi 仅在加载文档本身后才加载脚本。

因此,如果(例如)您在网络应用中使用 jquery 的 $(document).ready() ,则必须切换到 google.setOnLoadCallback()

keep in mind that google jsapi loads the scripts only after the document itself is loaded.

So, if (for example) you are using jquery's $(document).ready() in your web app, you'll have to switch to google.setOnLoadCallback().

野の 2024-08-02 03:12:09

我相信 Google JSAPI 也是异步的,有助于避免“收费站”,最好的描述是“想象一下你的网络浏览器和互联网本身之间有一条 4 车道的高速公路。这条高速公路经过优化,可以让图片、文本和 css 飞翔”但是,当涉及到外部脚本时,高速公路会创建一个收费站,从而减慢交通速度,最糟糕的是这些脚本后面的图片文本和 CSS 必须等待它们通过” - Andres Vidal

收费站。至关重要,必须始终避免。

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

The toll-booth is critical and must be avoided at all times.

妖妓 2024-08-02 03:12:09

我认为这种方法会对您有很大帮助,原因如下:

Google 使用内容交付网络,这将使远离您所在位置的用户可以比从您的网站更快地下载您的 jquery 库。

此外,它还会减少对服务器的请求,并使第一次用户从 google 的服务器下载 jquery javascript,如果用户已经在另一个具有这种实现的类似站点中,他将不需要再次下载它。

所以我认为这会对您的应用程序/网站有所帮助

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.

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.

So I think that this will help you app/site

木格 2024-08-02 03:12:09

这个文件压缩后是24KB,添加这样的文件会增加HTTP请求以及浏览器等待响应、执行和解析的时间...如果你说文件本身到处都被缓存,即使文件被缓存在浏览器,不要忘记考虑从磁盘读取、执行和解析所需的时间...

所有这些都只是为了获取 jQuery 文件或其他常见的 JS,我认为直接引用请求的资源最好

检查 Google 的最佳实践以获取更多信息。

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

all of this for only getting the jQuery file or other common JS, I think referring directly to the requested resource is better

check Google's best practices for more info.

你不是我要的菜∠ 2024-08-02 03:12:09

中国拥有 5 亿互联网用户,而且并不是唯一阻止 google api 的国家,这使得任何使用 http:// www.google.com/jsapi 功能失调。
有一个小的好处:由于异步加载技术,这些站点不会像其他使用直接引用的站点那样显示相同的挂起等待加载,例如:

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/jsapi dysfunctional.
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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文