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

来源:互联网 发布:ds1100k 网络键盘 编辑:程序博客网 时间:2024/06/10 03:42
  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}});
    }


原创粉丝点击