Error handling in jQuery.getScript

The jQuery getScript function does not seem to support the error callback function. I can't use ajax global error handling code here, a local error function would be ideal.

The documentation that the callback receives the / textStatus data seems to be wrong - the callback does not receive.

Any suggestions on how I found that the getScript call failed (for example, the server is unavailable)?

EDIT: just looked at the source and it seems that the callback is used only on successful completion, while the data is always set to null and textStatus is not defined (since I assume only the callback is for success only). The documentation is very incorrect for this function.

+6
javascript jquery ajax
source share
5 answers

It's a little hack, but ..

You can declare a variable inside loadable scripts and check it after you have loaded the script (provided that the full function is still working):

script_test.js:

var script_test = true; 

And then:

 $.getScript("script_test.js", function () { if (typeof script_test !== undefined) alert("script has been loaded!"); }); 

Or you could just try and see if there is something in your script that actually exists - functions, variables, objects, etc.



A more general way to do this is to add a self-executing function inside the scripts you want to load and make them execute the function in your "main" script:

main_script.js:

 function scriptLoaded(scriptName) { alert(scriptName + " loaded!"); } $.getScript("script_test.js"); 

script_test.js:

 (function () { scriptLoaded("script_test.js"); })(); 
-one
source share

As with jQuery 1.5, you can add .fail to your getScript call.

 $.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

+26
source share

For cross-domain script tags, a success event fires, but an error event is not executed; no matter what syntax you use. You can try this approach:

  • Create an error handler and run it after a few seconds using handle = window.setTimeout
  • Inside your success callback function, cancel the timeout using window.clearTimeout(handle)

Code example:

 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); }); 
+17
source share

Global jQuery Ajax-ErrorHandler will work!

Before installing $ .getScript-Call, an error handler will handle the error.

 $(document).ajaxError(function(e, xhr, settings, exception) { alert('error in: ' + settings.url + ' \n'+'error:\n' + exception ); }); 

As described in the jQuery manual: http://api.jquery.com/ajaxError/ .

+5
source share

jquery.ajax has an alternative way to handle errors

 jQuery.ajax({ type: "GET", url: 'http://www.example.com/script_test.js', dataType: "script", error: function (XMLHttpRequest, textStatus, errorThrown) { console.log('error ', errorThrown); }, success:function(){ console.log('success'); } }); 
+4
source share

All Articles