Closed SimplGy closed 10 years ago
I've been wanting this too. Either a flag in callback()
or a separate callback for errors would be useful. A flag in complete()
would be nice too.
I've been using yepnope to load all the scripts in a JS application I've been writing, displaying a progress bar as they complete and then displaying the application once everything is loaded. Unfortunately since callback()
and complete()
fire regardless of whether there was an error or not the app displays even when something fails to load. This generally breaks the app and gets really ugly when something like jQuery UI fails to load. I can usually tell when something hasn't loaded and just reload the page but some of my users may not be as perceptive about these things. It's a big enough issue that I've been considering ditching yepnope and just rewriting my loading code as a convoluted mess of jQuery.ajax()
calls.
You couldn't rewrite it as a jQuery.ajax
call because it's a script (unless you eval
everything). You'd have to use jQuery.getScript which also does not have an error callback. This is because there's no way to reliably get an error state back from loading a script. We considered adding the error state for the few browsers that this works in, however we felt this would encourage people to build apps that supported less browsers.
It wouldn't bother me too much if fewer browsers were supported, I tend to target only newer browsers anyway; the particular project that I mentioned already only works in a couple of browsers due to its use of IndexedDB. ;-)
I do understand your desire to support a wide variety of browsers though.
jQuery.getScript
actually just calls jQuery.ajax
with the dataType
set to "script"
(by way of jQuery.get
). Older versions of jQuery actually inserted a <script>
tag although looking at the 1.8 source it looks like it now evals the script via the globalEval
function.
I'm mobile, but they'd have to still inject script tags for cross domain scripts, no?
It seems every version of jQuery gets harder for me to read, so many things abstracted into undocumented functions that call undocumented functions... It looks like you are right, digging a little deeper into the built jquery.js it looks like the code that injects a <script>
tag has been moved into a call to jQuery.ajaxTransport
. I had trouble finding it in their repo; it is no longer in ajax.js where I expected it, it's now in ajax/script.js
I'd like to be able to tell when a file load fails. I see that there is a load limit and that it's configurable, but I do not see a way to check if the file was loaded. Right now the callback gets 'url', 'result', and 'key'. It gets all three whether the file was loaded or not. Result is based on the yepnope test, not the file loading, so that doesn't help.
There is no way to determine if a file loaded or timed out.