Google's JavaScript API Loader
Updated on November 17, 2023
By Pete Freitag
By Pete Freitag
Did you know Google's JavaScript Loader API can serve up popular javascript frameworks such as jQuery, and Prototype using their content distribution network? It works like this:
<script src="https://www.google.com/jsapi" language="javascript"></script> <script language="javascript"> google.load("prototype", "1.6"); google.load("scriptaculous", "1.8.2"); </script>
You can see a full list of supported API's here.
Their JavaScript API now gives you access to your visitor's location, it sets a variable google.loader.ClientLocation
, and populates it with City, Region/State, Country, Latitude, Longitude.
Here's an example:
<script src="https://www.google.com/jsapi" language="javascript"></script> <script language="javascript"> if (google.loader.ClientLocation != null) { document.write("Your Location Is: " + google.loader.ClientLocation.address.city + ", " + google.loader.ClientLocation.address.region); } else { document.write("Your Location Was Not Detected By Google Loader"); } </script>
The avaliable properties of the google.loader.ClientLocation
object are:
google.loader.ClientLocation.latitude
google.loader.ClientLocation.longitude
google.loader.ClientLocation.address.city
google.loader.ClientLocation.address.country
google.loader.ClientLocation.address.country_code
google.loader.ClientLocation.address.region
If a location cannot be determined from the visitors IP address, the google.loader.ClientLocation variable will be null
.
Google's JavaScript API Loader was first published on November 26, 2008.
If you like reading about google, javascript, geo, latitude, longitude, prototype, or scriptaculous then you might also like:
- Calculating Distance in Miles from Latitude and Longitude
- Howto Build Server Side AJAX Suggestions with script.aculo.us
- Geolocation API for Adobe AIR?
- GoogleBot Runs AJAX?
Discuss / Follow me on Twitter ↯
Tweet Follow @pfreitagComments
I wonder if relying on an external CDN (even if it is google who you'd think have fantastically reliable infrastructures) would be good for your site.
Especially if you are using heavy javascripts which do a lot 'onload'. This might provide a bad user experience in times where the network to google is a little slow.
With compression and expires headers you may really not be saving all that much. I guess it's a case by case basis.
I think it's great, but I don't think I'd use it for any of my sites.
Especially if you are using heavy javascripts which do a lot 'onload'. This might provide a bad user experience in times where the network to google is a little slow.
With compression and expires headers you may really not be saving all that much. I guess it's a case by case basis.
I think it's great, but I don't think I'd use it for any of my sites.
by Michael Sharman on 11/27/2008 at 1:21:25 PM UTC
@Michael I certainly understand your concerns, there are probably a lot of cases where you wouldn't want to use google's version.
As Andrew points chances are performance would be better using Googles version due to caching, and their network.
However if you are on an intranet using downloading google's version would probably take longer.
Also there is a security risk to including a javascript file from a foreign domain. If someone hacked google, or if google had an evil employee they could manipulate your website, and very easily steal sessions. It's actually quite a risk.
I may post some of these thoughts in another blog entry.
As Andrew points chances are performance would be better using Googles version due to caching, and their network.
However if you are on an intranet using downloading google's version would probably take longer.
Also there is a security risk to including a javascript file from a foreign domain. If someone hacked google, or if google had an evil employee they could manipulate your website, and very easily steal sessions. It's actually quite a risk.
I may post some of these thoughts in another blog entry.
by Pete Freitag on 12/02/2008 at 9:53:14 AM UTC
Thanks to Google API! The example can be found on http://checkbrowser.info by viewing the source codes.
by Poon on 10/13/2009 at 9:59:33 AM UTC
I think someone must be quite stupid to use Google Javascript. First the load and bandwidth it reduces is so small that it can be hardly measured today. Specially with server and hardware getting more powerful each day. You can load millions of request from a single server, so what would you save a 000000.1 load from using Googles javascript? Not to mention bandwidth. Its a penalty most people are going to use, just because they are to lazy to upload the javascript source to their servers, since they happen to be free. Just see how websites load slow because of Analytics, not to mention Gmail had several outages this year. All it takes is a little slow down to put your website to sleep. Imagine if each content on a page would rely on an external server. Each image, each javascript, each bit on a different server, it would be a mess. I would not even start promoting things like this. Its just a bad idea.
by henry on 12/01/2009 at 4:35:08 AM UTC
@henry ? I have to disagree. The primary benefit to loading from Google's server is the caching. The more websites that load the same file from the same CDN, the better the overall net experience for your visitors. It's taken a few years for the idea to catch on, but now that it has there's a good chance that someone coming to your site doesn't even need to load a specific file. Say for example Drupal with the jQuery Update module (http://drupal.org/project/jquery_update) installed, and the "load from Google's CDN" checked. Each of those Drupal installations is now configured to use the CDN, and going from one of those sites to another will not require additional bandwidth at all.
by Rob on 08/28/2012 at 3:53:37 PM UTC
(if you use the loader you have to put all your code in an onload handler and that might be annoying)