Handling errors in jQuery.getScript

For cross domain script tags, the success event fires but the error event does not; no matter what syntax you use. You can try this approach:

  1. Create an error handler and set it to fire after few seconds using handle = window.setTimeout
  2. Inside your success callback function, cancel the timeout using window.clearTimeout(handle)

Sample code:

var timeoutId; // timeout id is a global variable
timeoutId = window.setTimeout(function() {
    alert("Error");
}, 5000);
$.getScript("http://other-domain.com/script.js", function(){
    window.clearTimeout(timeoutId);
});

As of jQuery 1.5 you can append a .fail to your call to getScript.

$.getScript('foo.js', function(){
    //script loaded and parsed
}).fail(function(){
    if(arguments[0].readyState==0){
        //script failed to load
    }else{
        //script loaded but failed to parse
        alert(arguments[2].toString());
    }
})

http://api.jquery.com/jQuery.getScript/#handling-errors