基于prototype的动态加载js的一个类

  1. var  LinkFile = Class.create();
  2. LinkFile.prototype = {initialize: function  (_url, options) {
  3.      this .options = Object.extend({type: "script" , charset: "" , noCache: false , callBack: null }, options || {});
  4.      this .options.callBack = Object.extend({variable: null , onLoad: null , timeout:20000, timerStep:500}, options.callBack || {});
  5.      this .timer = 0;
  6.      this .loadTimer =  null ;
  7.      if  ( this .options.type ==  "script" ) {
  8.          //this.getJs(_url.trim());
  9.          this .getJs(_url);
  10.     }  else  {
  11.          //this.getCss(_url.trim());
  12.          this .getCss(_url);
  13.     }
  14.      if  ( this .options.callBack.variable) {
  15.          this .options.callBack.vars =  this .options.callBack.variable.split( "." );
  16.     }
  17. }, stop: function  () {
  18.     clearInterval( this .loadTimer);
  19.      this .loadTimer =  null ;
  20.      this .timer = 0;
  21.      return ;
  22. }, doCallback: function  () {
  23.      if  ( this .options.type !=  "script"  || ! this .options.callBack || ! this .options.callBack.vars || ! this .options.callBack.onLoad) {
  24.          this .stop();
  25.          return ;
  26.     }
  27.      this .timer +=  this .options.callBack.timerStep;
  28.      if  ($A( this .options.callBack.vars).any(
  29.      function  (v, i) {
  30.          var  _v =  this .options.callBack.vars.slice(0, i + 1).join( "." );
  31.          return  (eval( "typeof "  + _v +  "== /"undefined/"" ) || eval(_v +  "==null" ));
  32.     }.bind( this )) && ( this .timer <  this .options.callBack.timeout)) {
  33.          return ;
  34.     }  else  {
  35.         clearInterval( this .loadTimer);
  36.          if  ($A( this .options.callBack.vars).all( function  (v, i) {
  37.              var  _v =  this .options.callBack.vars.slice(0, i + 1).join( "." );
  38.              return  (eval( "typeof "  + _v +  "!= /"undefined/"" ) && eval(_v +  "!=null" ));
  39.         }.bind( this ))) {
  40.             ( this .options.callBack.onLoad)();
  41.         }  else  {
  42.              if  (( this .timer >=  this .options.callBack.timeout) &&  this .options.callBack.onFailure) {
  43.                 ( this .options.callBack.onFailure)();
  44.             }
  45.         }
  46.          this .loadTimer =  null ;
  47.          this .timer = 0;
  48.     }
  49. }, getJs: function  (_url) {
  50.      var  oHead = document.getElementsByTagName( "head" )[0];
  51.      var  _links = Element.getChildElementByTagName(oHead,  "SCRIPT" );
  52.     $A(_links).each( function  (s) {
  53.          if  (getFullUrl(s.getAttribute( "src" ) ||  "" ) == getFullUrl(_url)) {
  54.             Element.remove(s);
  55.         }
  56.     });
  57.      this ._link = document.createElement( "script" );
  58.      if  ( this .options.noCache) {
  59.         _url += (_url.match(//?/) ?  "&"  :  "?" ) +  "c="  + timeStamp();
  60.     }
  61.      this ._link.src = _url;
  62.      this ._link.type =  "text/javascript" ;
  63.      if  ( this .options.charset) {
  64.          this ._link.charset =  this .options.charset;
  65.     }
  66.      if  ( this .options.callBack) {
  67.          this .loadTimer = setInterval( function  () {
  68.              this .doCallback();
  69.         }.bind( this ),  this .options.callBack.timerStep);
  70.     }
  71.     oHead.appendChild( this ._link);
  72. }, getCss: function  (_url) {
  73.      var  oHead = document.getElementsByTagName( "head" )[0];
  74.      var  _links = Element.getChildElementByTagName(oHead,  "LINK" );
  75.     $A(_links).each( function  (l) {
  76.          if  (getFullUrl(l.getAttribute( "href" ) ||  "" ) == getFullUrl(_url)) {
  77.             Element.remove(l);
  78.         }
  79.     });
  80.      this ._link = document.createElement( "link" );
  81.      if  ( this .options.noCache) {
  82.         _url += (_url.match(//?/) ?  "&"  :  "?" ) +  "c="  + timeStamp();
  83.     }
  84.      this ._link.href = _url;
  85.      this ._link.type =  "text/css" ;
  86.      this ._link.rel =  "stylesheet" ;
  87.      if  ( this .options.charset) {
  88.          this ._link.charset =  this .options.charset;
  89.     }
  90.     oHead.appendChild( this ._link);
  91. }};


  92. 使用方法如下:
    1 ===========
    function search(){
            new LinkFile(url, {
                                            type: 'script',
                                            callBack: {
                                                    variable: 'search_result',
                                                    onLoad: show_result.bind(this),
                                                    onFailure: function(){},
                                                    timeout: 50000
                                                    /*timerStep: 500*/
                                            }});
    }

  93. 2 ============

    function showresult(){
        new LinkFile(commentjspath, {type: 'script',callBack: {variable: 'comments',
                                onLoad: function(){loadJS(commentjspath);showComments(6,1, 6 ,"",<%=articleid%> );},
                                onFailure: function(){document.getElementById("updatecontent").innerHTML=""; $("commentCount").innerHTML = "("+0+")"; },
                                timeout: 10000}});
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值