19

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?
Community
  • 1
  • 1
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
  • Including an external script in an HTML causes blocking request in the browser and create a single-point of failure. For more info please see https://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/ – vhs Jun 11 '17 at 05:10

8 Answers8

24

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.

Keith Bentrup
  • 11,834
  • 7
  • 49
  • 56
17

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.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • 1
    Same in China. To avoid having the websites I visit not loading because of a call to google.com, I installed all the Google JS libraries in local, and redirected ajax.googleapis.com to localhost. But it doesn't work with this call : http://www.google.com/jsapi So I would recommend using ajax.googleapis.com directly, and not loading the js file via the google "load" command. – hellimac Jul 29 '15 at 06:25
15

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.

vhs
  • 9,316
  • 3
  • 66
  • 70
Fusspawn
  • 1,405
  • 12
  • 19
  • 5
    It's a nice benefit though. One less file to serve from your server, and the client may not have to download it at all because the more people that use this method, the greater the affect. It increases the chance that it will already be in their cache. Faster sites = happier visitors. – Keith Bentrup Jul 09 '09 at 22:35
  • Oh its an awesome benefit, I use it every chance i get, ( while keeping a copy locally for whem my inet die's and i still feel like coding. ) Im just keeping my fingers crossed the google CDN never drops, thatll brake a ton of websites. – Fusspawn Jul 09 '09 at 22:38
12

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

Amir Arad
  • 6,724
  • 9
  • 42
  • 49
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.

IEnumerator
  • 2,960
  • 5
  • 31
  • 33
  • Interesting analogy. Put another way the _toll booth_ is design flaw which violates the purpose of having a distributed Web - fault tolerance. – vhs Jun 11 '17 at 05:15
3

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

Cesar Hermosillo
  • 942
  • 7
  • 19
1

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.

Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
1

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:

Jon
  • 21
  • 1
  • OP shows initial request being made is external, so it indeed blocks and causes a potential single-point-of-failure. – vhs Jun 11 '17 at 05:16