Jun 23

CDN加速:如何自动选择最快的镜像 不指定

felix021 @ 2008-6-23 19:22 [IT » 网络] 评论(1) , 引用(0) , 阅读(6690) | Via 本站原创 | |
(from Slyar)
  我用的是梦游科技置在上海移动机房的虚拟主机,梦游科技免费赠送几个CDN加速镜像,包括电信网通联通的,使用这些镜像确实可以加速访问,效果很好,但是不同的镜像需要不同的域名,非常麻烦,别人问我Blog的时候我总得问问对方你是网通的还是电信的,然后再给出相应的域名。后来发现Jannick的Blog首页有一个自动选择最快镜像的功能,于是问了一下,拿到代码,非常简单,但是没看懂:没看到测速的地方啊。于是再问,Jannick自己也看了半天,终于看懂了,解释了一下,发现确实很简单。
代码如下:
<script type="text/javascript">
first = 1;
var autourl=new Array(); //定义域名数组
autourl[1]="www.slyar.com/blog";
autourl[2]="cnc.slyar.com/blog";
autourl[3]="tel.slyar.com/blog";
autourl[4]="uni.slyar.com/blog";
function auto(url){
  if(first){//如果是第一个onerror事件
    first=0; //以后的onerror事件就不会跳转
    top.location=url; //跳转
  }
}
function run(){
  for(var i=1;i<autourl.length;i++) {
    //写入一个不存在的img,哪一个速度快,哪一个最先返回错误,触发onerror事件产生跳转
    document.write("<img src=http://"+autourl[i]+" width=1 height=1 onerror=auto('http://"+autourl[i]+"')>")
  }
}
run();//运行run函数
</script>
Wait a moment please....


解释:见代码注释。

Felix将代码小做修改如下:
<script>
var names = new Array;
names[0] = "19880711.com";
names[1] = "ctc.19880711.com";
names[2] = "cnc.19880711.com";
function run1(){for(i = 0; i < names.length; i++)
  document.writeln('<img style="display:none" src="http://'+names[i]+'" onerror="window.location=\'http://' + names[i] + '/blog\'"/>');
}
</script>
<a onclick="run1()">点击这里选择最快的镜像</a>




欢迎扫码关注:




转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php
slyar Homepage
2008-7-10 11:09
呜呜呜呜……我换名字了,不要旧名字,换掉,换掉~
felix021 回复于 2008-7-10 16:18
好吧。。我知道了。。
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]