13

In my site I have:

...
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
...

The script above is the Google script to load up other resources dynamically. (eg Google charts API)

This works 99.99% of the time. However, I just got a client that for some reasons got his company restricting access to google.com.

As a consequence of this my website simply threw a JavaScript error.

Now I know how to handle that, and I can check if window.Google exists. but my question is

"what's the standard way to deal with this? "

In other words if you embed 3rd party JavaScript how best do you deal with their JS not available?

NOTE: VERY IMPORTANT

You can not host the chart code locally or on an intranet.

SEE FAQ from Google: https://developers.google.com/chart/interactive/faq#localdownload

Can I download and host the chart code locally, or on an intranet?

Sorry; our terms of service do not allow you to download and save or host the Google.load or Google.visualization code.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Zo72
  • 14,593
  • 17
  • 71
  • 103
  • Not knowing anything about the Google API in particular, can you not just host the file from your own system, or do you rely on any updates that might happen to the Google hosted file? – freefaller Oct 29 '12 at 11:31
  • To be fair Google API CDN is very very reliable. Prolly much more reliable than your own server. – specialscope Oct 29 '12 at 11:37
  • @freefaller no, I can't that's because you can not host locally the google api. – Zo72 Oct 29 '12 at 11:50
  • 2
    @specialscope of course google api cdn is super reliable... that's not the problem though... – Zo72 Oct 29 '12 at 11:54
  • 3
    This is a really good architectural question! I'm glad I am not the only person who thinks about such issues! – Andrew Rhyne Nov 13 '12 at 21:41
  • for me it's quite simple .... even an API availability of 99.99% (which is a good value for highly available data centers) will degrade the service availability of the site - in addition to any network path availability (usually <= 99.9%) ... so thinking about a "graceful degradation" is **key** therefore +1 to the question and Andrew's comment – MikeD Nov 20 '12 at 13:58

10 Answers10

10

There is no real alternative. Due to Google's terms of service you cannot use Google API without access to google.com.

  • Check the connection to Google and iform user that function is not available
  • Develop your own or use non-google api. Still you can use Google if available
Jan Pfeifer
  • 2,854
  • 25
  • 35
  • @Zo72 Did my solution not work for you? If now, I would be interested why this is not possible? – Uooo Nov 09 '12 at 05:20
  • @Jan Pfeifer you might be right. I will tag this is as the best solution unless somebody does not come back with a better one – Zo72 Nov 09 '12 at 15:55
9

The solution is that your client's company review their content filtering policies. Google are quite clear in their previous answer concerning offline access:

…your computer must have live access to http://www.google.com/jsapi in order to use charts.

You are using a third-party solution according to their terms and conditions, which naturally imposes limits on how that solution may be used by your clients. You need to stand firm or find a more liberally-licensed solution. (At any rate, you are more likely to succeed at convincing your client's IT department than petitioning Google to change their TOS.)


For the more general case of third party JS APIs that may not load but for which you are allowed to keep a local copy on your server, see this question.

Community
  • 1
  • 1
Jordan Gray
  • 16,306
  • 3
  • 53
  • 69
7

You can try it like this:

Instead of using the direct link to the Google libraries you want to use, use a link which points to your server:

<script type="text/javascript" src="https://www.myserver.com/jsapi"></script>

When your server gets an incoming request to this URL, your server now makes a request to Google to get the API and sends the response to the client.

That means you do not install the API anywhere locally or on a server and always get the most actual version directly from Google. People also do not need access to Google (as in the company you mentioned) and therefore can use your service.

Uooo
  • 6,204
  • 8
  • 36
  • 63
  • there are a few errors in this solution. Note: the google api returns javascript which in turn refer to other javascripts and resources (e.g.: gif/jpg, css) which refer to google.com so it would not work. – Zo72 Nov 09 '12 at 15:54
  • 1
    What are the errors in this solution? You could replace occurences to google.com by myserver.com and go the same way with them again. I know this is quite a big workaround, but I think it could work. – Uooo Nov 10 '12 at 08:48
  • 2
    @w4rumy No, it is not possible. You'd have to crawl through the [JSAPI code](https://www.google.com/jsapi) before executing and replace all references to google (but not objects named google)... take a look at that code. To say that it would be complex would be an understatement, and you'd be breaking the Google TOS. – joequincy Nov 13 '12 at 21:06
  • @joequincy Haha! Yes you have to crawl through the JSAPI code, and change every "google.com" to "myserver.com". But that is still nothing complicated even if it has to be done 1023874 times. Of course you could go a much easier way and simply change the DNS entry for "google.com" in the hosts file. And I dont think you'd be breaking the Google TOS, since you're still not saving the code. – Kenyakorn Ketsombut Jul 08 '13 at 04:21
  • 1
    @joequincy But if you want to be very fussy about that: every company that uses a caching proxy server can't use Google Charts legally. Also every kind of browser cache is against the TOS. So using it with Chrome, Firefox or IE is also not allowed. As a matter of fact, the product violates itself, because the load() function causes modern browsers to download, save and execute the code locally, while saving it locally is not allowed :/ – Kenyakorn Ketsombut Jul 08 '13 at 04:22
  • 1
    @KenyakornKetsombut Changing `hosts` wouldn't work practically, as you'd have to change it for every workstation accessing the application (and a company that blocks access to Google probably doesn't allow users to modify `hosts` anyway). Also, you're confusing terms: it is not saved locally, merely loaded into memory. End-user browser caches are not considered to be violating, as they still ping the Google server to check whether the cached version is outdated. The inherent behavior of the browser serves only to save bandwidth if the file is current, not to circumvent Google's updates. – joequincy Jul 08 '13 at 05:40
3

Use Firebug or the Chrome Dev Tools to inspect your HTML source once the charts scripts are loaded. Access the scripts in your browser and save them locally, then serve them from your own server. This isn't recommended, of course, but if you don't have any other choice...

For example, checking the code of one of the pages I use it on, the core script for the Google Charts library is located at: https://www.google.com/uds/api/visualization/1.0/3d781368978b51b3ca00a01566dccf40/format+en,default,corechart.I.js

Victor Stanciu
  • 12,037
  • 3
  • 26
  • 34
  • 2
    no, I can't that's because you can not host locally the google api. IT's ILLEGAL according to their terms. (I will edit my question to make it more clear) – Zo72 Oct 29 '12 at 11:50
3

Use the javascript window.onload to check whether the api has loaded or not, if no then load it from your server.

SutharMonil
  • 78
  • 3
  • 19
3

You already know how to check whether or not your library has been loaded (checking the object), if it fails, than what you can do within giving constrains:

Keep checking the object with timer and trying to download library, displaying message for a user

In case first one fails, you have two ways again: Stopping your application and displaying an error: "Application error... try later" Or downloading different library as a fallback

Roman
  • 504
  • 4
  • 10
  • but yours are all just little workarounds ... I need a better way of solving this. In fact arguably 'try later' might never work... – Zo72 Nov 07 '12 at 10:43
3

Are you progressively enhancing or gracefully degrading the page? If so, what do you display to users without JavaScript for this chart? A table? A list? This is what you should leave in the page and only start changing it once google's JS is available. Either that, or find an alternative library like raphaeljs that lets you keep all your code within your project.

Dawn
  • 941
  • 6
  • 11
  • thanks for your observation. I 'd be gracefully degrade my page. However for no JS there is just a big warning: 'hey you need js enabled for this app to work' – Zo72 Nov 12 '12 at 13:58
  • people don't support javascript? what is this 2005? – Dnaso Nov 13 '12 at 17:32
  • Ya I don't typically market my software towards people using devices that don't support Javascript. That seems like a bit of an antiquated approach. In '06 it was definitely a concern, but even the most rudimentary mobile devices tend to support JS, and if they don't then they probably won't have much use for my apps as they tend to be Web 2.0 and targetted towards the mainstream. – Andrew Rhyne Nov 13 '12 at 21:39
  • Search engine bots don't tend to support JS all that well.A decent mobile will support JS now, but when they launched they didn't, so there's always the possibility for disruption. – Dawn Nov 22 '12 at 12:00
3

IF (BIG IF) you are not worried about the interactivity the Google Charts and want to display them to the user just to see - maybe add your own javascript to it but not depend on the Google Javascript at all, this can turn the google charts into a image that you can display to the user.

Also this requires access to install a command line tool on the server.

http://code.google.com/p/wkhtmltopdf/ is a command line tool that will generate an image from an html page. If you build a simple page that only shows the chart you want and point the wkhtmltoimage tool at the local html file it will load the Google Charts javascript and generate the chart then generate an image out of the results.

YES I understand this is VERY kludgy and is adding a big tool for a small problem but with the browser restriction and the Google Terms of Service this will solve most of the problem.

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35
2

You can try going straight to google and if it fails (if google is restricted) you can bounce the request off of your server which forwards the request using CURL to google. If that doesn't work then Google is most likely down. This should cover the issue that you described in your question, but there isn't really a fix for if google itself actually goes down. It should, however, give your application access around domain restrictions because the request will be routed to your server rather than straight to google. I use this architecture for all requests so that I don't have ajax requests routed to random servers. It allows me to control what interacts with my front end using my backend. There are other benefits to this, especially if you are using something like AngularJS with NodeJS because you can decouple a lot of your third party libraries. This however, is beyond the scope of your question!

Basically, it works like this (pseudo code):

If(!Browser->Google->Browser){
    return Browser->MyServer->Google->MyServer->Browser;
}  
Andrew Rhyne
  • 5,060
  • 4
  • 28
  • 41
1

An answer has been accepted already, but still I would like to leave an additional aspect elaborating on the comment I made above ....

It has been accepted that the Google Server is the only place from where the API can be loaded. We don't know whether the client's IT manager will re-think their content policy, they might have good reasons for that.

Given a non-100% availability of all the components along the path between a user browser and the Google API, sooner or later a user will end up in an error situation; statistically this is unavoidable.

What is not acceptable (and avoidable) for a user is to receive an "unspecific" JS error making him/her believe there's a bug on the page. So my solution would be to trap the failure loading the Google API and display a message "Third party components temporarily unavailable - Please try later".

This will demonstrate to the user that

  1. we know what's going on
  2. there's nothing we can do about it now
  3. but it's not totally unexpected and still somehow under control
MikeD
  • 8,861
  • 2
  • 28
  • 50