1

I have a Google +1 button on my site that requires the following script:

<script src="https://apis.google.com/js/platform.js" async defer></script>

The problem is as of today, apis.google.com can't be pinged from everywhere (servers are down) and some of my users don't see a button. I don't always want to use my own copy of the script because I can see that breaking the +1 functionality at some point. I'd like to use something like the solution from this question: my server's fallback copy should be fetched only when the CDN fails.

What's a good way to do that for this script? Is there a generic way to do this for any remote script using jQuery or plain js? Thanks in advance.

EDIT: I tried jQuery getScript() and wrapped it in a function like this:

function fetch_script(script,fallback) {
    $.getScript( script )
        .fail(function() {
            $.getScript( fallback );
    });
};

This does not always work and is not reliable. Two problems I found:

  1. 500 errors trigger the fail method, but 404 errors do not and the fallback script is not loaded in these cases. Not sure what happens if the server is just down.
  2. Some scripts are fetched (I see this in the console) but are somehow not executed (or they fail silently). Here's a script that doesn't work.
Community
  • 1
  • 1
kontextify
  • 478
  • 5
  • 16

0 Answers0