Let’s imagine a web page page needing to load a javascript file (i.e. my.js). Is it possible to organize the following fail-over loading sequence?
- If server A is up, load
my.jsfrom server A. - Else, if server B is up, load
my.jsfrom server B. - Else, if server C is up, load
my.jsfrom server C. - …
If yes, how to proceed? Thanks.
P.S.: I have just found yepnopejs. Does anyone recommend it?
I have seen this technique to allow a fallback if a CDN is down.
If your js file has some testable property like a global variable (I’ve called it marker), you can attempt to load the file from server A, test for the marker and if it is not found script another attempt.
Update
There is no danger that all the scripts will run using this technique. John Resig explains this in a blog post.. Scripts can download in parallel and in any order but they must execute in order.
Here is a fiddle that demonstrates