脚本绑定回调增强版:备用url可以失败重试

脚本绑定回调增强版:备用url可以失败重试
4年前写过一篇 《脚本绑定回调》  进行了一些有趣的尝试,这些尝试现在在一些web产品中已经应用了好几年了。这两年随着海外用户的增多,用户情况的复杂化,我们的服务部署也开始复杂化了,有一些用户访问A域名失败,访问B域名就可能很畅顺,另一些用户则相反。而且很多时候这并不是gslb这样的调度可以及时检测到和快速调整的,不由得想想,能不能进行失败重试呢?其实也很简单把4年前的代码改了一改,做了一个原理性的实验:

< HTML >
< HEAD >
< SCRIPT  LANGUAGE ="JavaScript" >
var  isIE  =   !! window.ActiveXObject;
var  useFragment = false ;
function  loadjs(url,callback,errcallback,url2,url3){
    
if (isIE){
        
if (useFragment){
           
var  df  =  document.createDocumentFragment();    
            df.visitCountCallBack 
=   function (data){
                s.onreadystatechange
= null ;
                df
= null ;
                callback(data);
            }
            
var  s  =  df.createElement( " SCRIPT " );
            df.appendChild(s);
            s.onreadystatechange
= function  (ec,cb,u2,u3){
                
return   function (){
                    
if (s.readyState == " loaded " ) {
                        s.onreadystatechange
= null ;
                        df
= null ;
                        
if ( ! u2){
                            ec();
                        }
else {
                            loadjs(u2,cb,ec,u3)
                        }
                    }
                }
            }(errcallback,callback,url2,url3)
            s.src 
=  url;
        }
else {
            
var  i = new  ActiveXObject( " htmlfile " );
            i.open();
            i.parentWindow.visitCountCallBack
= function (i){
                
return   function (d){
                    i.parentWindow.errcallback
= null ;
                    i
= null ;
                    callback(d);
                }
            }(i);
            i.parentWindow.errcallback
= function (ec,cb,u2,u3){
                
return   function (){
                    i.parentWindow.errcallback
= null ;
                    i
= null ;
                    
if ( ! u2){
                        ec();
                    }
else {
                        loadjs(u2,cb,ec,u3)
                    }
                }
            }(errcallback,callback,url2,url3)
            i.write(
" <script src=\ "" +url+ " \ " ><\/script><script defer>setTimeout(\ " errcallback()\ " ,0)<\/script> " )
            
if (i)i.close(); // 如果数据被cache,运行到这一行的时候有可能回调已经完成,窗口已经关闭。
        }
    }
else {
        
var  i  =  document.createElement( " IFRAME " );    
        i.style.display
= " none " ;
        i.callback
= function (o){
            callback(o);
            i.contentWindow.callback
= null ;
            i.src
= " about:blank "
            i.parentNode.removeChild(i);
            i 
=   null ;
        };
        i.errcallback 
=   function (ec,cb,u2,u3){
            
return   function (){
                    
if ( ! u2){
                        ec();
                    }
else {
                        loadjs(u2,cb,ec,u3)
                    }
            }
        }(errcallback,callback,url2,url3);
        i.src
= " javascript:\ " < script > function  visitCountCallBack(data){frameElement.callback(data)}; < \ / script >< script src = ' " +url+ " ' >< \ / script >< script > setTimeout('frameElement.errcallback()', 0 ) < \ / script > \ "" ;
        document.body.appendChild(i);
    }
}

function  init(){
    
var  spans  =  document.getElementsByTagName( " span " );
    
for ( var  i = 0 ;i < spans.length;i ++ ){
        
var  id  =  spans[i].id;
        
var  url  =   " http://g.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin=a " + id; // 故意制造错误引发重试
         var  url2  =   " http://g.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin=b " + id; // 故意再次制造错误引发重试
         var  url3  =   " http://g.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin= " + id;
        
var  callback  =   function (id){  return   function (data){
            document.getElementById(id).innerHTML 
=  data.visitcount;
            }
        }(id);
        
var  errcallback  =   function (id){  return   function (){
            document.getElementById(id).innerHTML 
=   " 无法连接到服务器 " ;
            }
        }(id);
        loadjs(url,callback,errcallback,url2,url3);
    }
}
</ SCRIPT >
</ HEAD >
< BODY  onload ="init()" >
12345(非法帐号)的访问量:
< span  id ="12345" ></ span >< BR >
123456 的访问量:
< span  id ="123456" ></ span >< BR >
20050606 的访问量:
< span  id ="20050606" ></ span >< BR >
</ BODY >
</ HTML >

故意在前两次请求中制造了错误,尝试到第三个url的时候才成功。

你可能感兴趣的:(脚本绑定回调增强版:备用url可以失败重试)