tags:

views:

543

answers:

3

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

Documentation that the callback gets data/textStatus seems incorrect - the callback gets neither.

Any suggestions on how I detect that a call to getScript failed (server not available, for instance)?

EDIT: Just looked at source, and it seems like the callback is only invoked on success, with data always set to null and textStatus not defined (since it's a success-only callback, I presume). The documentation is very incorrect for this function.

A: 
$.getScript("your_script.js", function (data, textStatus) {
    if (textStatus === "success") {
        // do your stuff
    } else {
        // error
    }
});
yoda
Heh, you might as well have said RTFM. Unfortunately, the documentation is wrong here. There is no data/textStatus passed to the callback. On success, the resulting js seems to be simply evaluated. I tried this before I posted - updated my question too.
psychotik
Use $.ajax instead then: http://docs.jquery.com/Ajax/jQuery.ajax#options
yoda
A: 

This is a bit of a hack, but..

You could declare a variable inside the scripts you load and check for it after you've loaded a script (assuming that the complete-function still fires):

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 whatever is in your script, actually exists--functions, variables, objects, etc.


A more generic way to do this would be adding a self-executing function inside the scripts you want to load, and make them execute a 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");
})();
roosteronacid
yea, this is what I'm doing currently but like you said, it's a bit of a hack. Btw, the code should read <code> if ( typeof script_test !== "undefined" ) ... </code>
psychotik
Aye, it's a hack. Did you consider my 2nd example? And.. I don't believe you need the typeof operator.
roosteronacid
Thanks for that second suggestion. Also, unless I'm really going nuts, you will need the typeof if the var isn't defined anywhere already. Try running this function below in your script (make no mention of foo before). You'll notice an error in the js console: function test(){ if (foo === undefined) alert("Yay!"); }
psychotik
+1  A: 

I think the real answer is don't use getScript!

Try something like this instead:

$.ajax({
  url: 'http://yoururl.com',
  dataType: 'script',
  error: function() {
    handle that error
  }
});

getScript is only a shortcut, and unfortuneately, in this case it can't support what you'd like to do.

Ben
This doesn't work when you're going cross-site to get your script. $.getScript does.
psychotik
Actually scratch that - this does go cross site, but doesn't invoke the error function on failure.
psychotik