Reputation: 933
I'm trying to load an external JavaScript file via jQuery's .ajax() function (tried .getScript(), but it has the same problem).
According to the documentation, and testing, it doesn't fire error events, or even any of the global AJAX events when there is an error when loading script's from a remote source.
As far as I can see, the only event that is fired in the success event.
My question is: how can I detect if the remote script fails to load.
I can't alter the remote script, but I can detect if it has loaded. (By checking if a variable is defined).
Upvotes: 0
Views: 409
Reputation:
jQuery "helpfully" evaluates scripts for you. If you want to avoid this, you can set manually set the dataType
:
$.ajax({
url : '/foo'
, dataType : 'text'
, success : function(){ ... }
});
which will prevent jQuery from evaluating the script. You can then eval
it yourself, wrapped in a try/catch
.
Upvotes: 1
Reputation: 41757
Set a Timeout for a period of time, and in the success callback cancel the timeout. If the timeout fires then you can cancel the request and try again and / or perform your error handling.
Upvotes: 1