Should I use Google's JSAPI in production code

2020-02-19 06:13发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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.



回答6:

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



回答7:

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.



回答8:

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: