1

I've isolated the issue here:

http://jsfiddle.net/ajkochanowicz/pEnMw/2/

Basically, I'm trying to add a feature in my script to look for a local version of the .js file if the CDN-hosted one fails. So it seems appropriate to load that script in .done or error or complete, but none of those work.

Clarification: Looking for a solution which works with user-defined js files, so testing for individual functions in each is not feasible.

There are a lot of questions with this similar topic already on SO, but all that I've found are about getting the request to work at all, or handling JSON(P) requests. :( I'm interested in error handling with .js files.

Thanks.

Adam Grant
  • 12,477
  • 10
  • 58
  • 65

3 Answers3

3

There's no way to know if a script element failed to load, so the only solution is to add a timeout.

Quoting jQuery's ajax documentation (settings.error section):

error(jqXHR, textStatus, errorThrown)

[....]

Note: This handler is not called for cross-domain script and JSONP requests.

I've updated your fiddle to include the timeout:

$.ajax({
    type: "GET",
    url: urlVar,
    dataType: 'script',
    timeout : 5000,
    success: function(data) {
        alert('it worked');
    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert('it did not work');
    },
});
Alessandro Vendruscolo
  • 14,493
  • 4
  • 32
  • 41
  • I'll try the timeout, but I wonder if there's a more clever way to do this...From the console, I can see that the client at least gets an error. It may not be $.ajax that receives it but if it gets to the client somehow, there ought to be a way to parse that error and do something with it. – Adam Grant Jul 20 '12 at 15:42
  • 1
    jQuery uses JSONP for cross-domain requests. See http://stackoverflow.com/questions/2067472/please-explain-jsonp for the explanation of what's going on, and thus why you lose control of the request. – Jerod Venema Jul 20 '12 at 15:58
  • Ah, that makes sense. Although I think there may be something really hacky one could do here. Whatever else doesn't work with the JSONP request, the client does receive an error msg, "[url] A server with the specified hostname could not be found". If only there was a way to hijack the native function which calls that error message and parse the error string... – Adam Grant Jul 20 '12 at 16:10
0

The error handler in $.ajax is not called for cross-domain script and JSONP requests.

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

A solution that can work, supposing the library you want is jquery :

In the header :

   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

In your body :

   <script>
       window.onload=function(){
          if (!$) { // jquery isn't available even after the window is fully loaded, so the distant loading failed
            var head= document.getElementsByTagName('head')[0];
            var script= document.createElement('script');
            script.type= 'text/javascript';
            script.src= 'myOwnJquery.js';
            script.onload = someCallback; // <- might be usefull
            head.appendChild(script);
          }
      }
   </script>

Replace $ in this example by a function of the library you want to test.

The main reason to do it like this is that you can set a callback, so you don't try to do now what can't be done before you have your library.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758