从服务器序列加载.js的故障转移? [英] Failover loading of .js from sequence of servers?

查看:107
本文介绍了从服务器序列加载.js的故障转移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们想象一个需要加载javascript文件的网页(即 my.js )。是否可以组织以下故障转移加载序列?

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?


  1. 如果服务器A已启动,请加载 my。来自服务器A的js

  2. 否则,如果服务器B启动,则从服务器加载 my.js B。

  3. 否则,如果服务器C启动,则从服务器C加载 my.js

  4. ...

  1. If server A is up, load my.js from server A.
  2. Else, if server B is up, load my.js from server B.
  3. Else, if server C is up, load my.js from server C.
  4. ...

如果是,请继续操作?谢谢。

If yes, how to proceed? Thanks.

P.S。:我刚刚找到了 yepnopejs 。有没有人推荐它?

P.S.: I have just found yepnopejs. Does anyone recommend it?

推荐答案

我已经看到这种技术可以在CDN关闭时允许回退。
如果你的js文件有一些可测试的属性,比如一个全局变量(我称之为标记),你可以尝试从服务器A加载文件,测试标记,如果找不到脚本,则另一次尝试。

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.

<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>

更新
使用此技术不会运行所有脚本。 John Resig在博文中解释了这一点。。脚本可以按任何顺序并行下载,但必须按顺序执行。

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.

这是一个演示的小提琴

这篇关于从服务器序列加载.js的故障转移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆