Failover loading .js from server sequence?

Imagine the web page page needed to load a javascript file (i.e. my.js ). Is it possible to organize the following sequence of boot crashes?

  • If server A is inserted, download my.js from server A.
  • Otherwise, if server B is up, download my.js from server B.
  • Otherwise, if server C is down, download my.js from server C.
  • ...

If so, how to proceed? Thanks.

PS: I just found uppages . Does anyone recommend this?

+4
source share
1 answer

I saw this technique to allow backtracking if the CDN is not working. If your js file has some kind of testable property, for example a global variable (I called a marker), you can try to download the file from server A, check the marker and if it is not found, the script will try again.

 <script type="text/javascript" src="http://server_A.tld/my.js"></script> <script type="text/javascript"> if( !window.marker ) { document.write( '<script type="text\/javascript" src="http:\/\/server_B.tld\/my.js"><\/script>' ); } </script> 

Update There is no danger that all scripts will work using this technique. John Resig explains this in a blog post. . Scripts can be loaded in parallel and in any order, but they must be executed in order.

Here is a fiddle that demonstrates

+3
source

All Articles