As far as I know, requirejs
is usually used like this:
require(['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'],
function() {
// Code that depends on jQuery here.
}
);
Function that depends on jQuery will be called only when jQuery is loaded. But if jQuery fails to load, code that depends on it will never be executed.
Since you want to try and use local jQuery in this case, you should probably catch the script load timeout error and try to load jQuery from another source. (But note that timeout errors are slow.)
There's little information on error handling in docs:
To detect errors, you can override require.onError()
to get errors. The error object passed to the onerror function will contain two properties if it is a timeout issue:
- requireType: value will be "timeout"
- requireModules: an array of module names/URLs that timed out.
I think, the code may look like this (not tested):
var functionThatDependsOnJquery = function() {
// Code that depends on jQuery here.
};
require.onError = function(err) {
# If we have Google jQuery load timeout...
if (err.requireType = "timeout" &&
err.requireModules.indexOf('http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js') != -1
) {
# ...we'll try to load it from another source.
require(['/scripts/localJquery'], functionThatDependsOnJquery);
}
# We have another script load timeout, so we just throw an error
# as requirejs normally does.
else { throw err; }
};
# Try to load Google jQuery.
require(['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'],
functionThatDependsOnJquery);