轻量级Javascript 框架

来源:互联网 发布:商标侵权赔偿额计算法 编辑:程序博客网 时间:2024/05/17 07:15

g.js      location /jslib/g.js

  LICENSE ISSUES
  ==============

globle framework , open source & free, Powered by barenx,  Licensed on GPL

//Copyright(C) 2007-2008. BAREN Productions. All rights reserved.
var __barenx__lib__globle_location="/jslib/g.js";
//#ifndefine g_js
if('undefined'==typeof __barenx__lib__globle){
//#define g_js
__barenx__lib__globle=function(){
/*
============================================
javascript framework by barenx
make simply use & easy code in javascript 
============================================
*/

/*    const declare  */
this._L="length";this._P="prototype";this._D="document";this._B="body";
this._U='undefined';this._O='object';this._S='string';this._f='function';
if(_U==typeof this.event)this.event=false;
var _w=this;
//frame work name space _F
this._F={
//const
ver:3.61,
auther:
"barenx",
IE:document.all
&&window.ActiveXObject&&!window.opera,
n:(
function(){}),
f:(
function(){return false}),
w3c:(_f
==typeof document.getElementById&&_f==typeof document.createElement),
$:
function(a,b){b=b||_w;return b[_D].getElementById(a)},
$$:
function(a,b){b=b||_w;return b[_D].frames?b[_D].frames[a]:this.$(a,b).contentWindow},
$n:
function(a,b){b=b||_w;if(b[_D])b=b[_D];return b.getElementsByTagName(a)},
$a:
function(a,b){if(_S==typeof a)a=this.$(a);return a.appendChild(b)},
$r:
function(a){if(_S==typeof a)a=this.$(a);return a.parentNode.removeChild(a)},
$u:
function(a,b){if(_S==typeof a)a=this.$(a);if('INPUT'==a.tagName)a.value=b;else a.innerHTML=b;return a},
$h:
function(a,d){if(_S==typeof a)a=this.$(a);if(d)a.style.display='none';else a.style.visibility='hidden';},
$s:
function(a){if(_S==typeof a)a=this.$(a);a.style.display=a.style.visibility=''},
$t:
function(a,b){b=b||_w;return b[_D].createTextNode(a)},
$w:
function(a,b){b=b||_w;b[_D].writeln(a)},
$c:
function(a,c,b){b=b||_w;if(b[_D])b=b[_D];
    
var o=b.createElement(a);
    
if(c){for(var k in c){
    
switch(typeof(c[k])){
        
case 'object':this.e(o[k],c[k]);break;
        
case 'function':o[k]=(function(){c[k].apply(o,arguments)});break;
        
default:o[k]=c[k];break;
    }

    }
}

    
return o;
}
,
$l:
function(o,f,e){
    
if(o.addEventListener)o.addEventListener(e,f,false);
    
else if(o.attachEvent)o.attachEvent("on"+e,f)
}
,
$q:
function(o,f,e){
    
if(o.removeEventListener)o.removeEventListener(e,f,false);
    
else if(o.detachEvent)o.detachEvent("on"+e,f)        
}
,
//function
c:function(a,b){for(var p in a)a[p]=b[p];return a},
e:
function(a,b){for(var p in b)a[p]=b[p];return a},
b:
function(f,o,ev){return function(e){f.call(o,e||ev)}},
g:
function(){
    
var h=false;
    
try    {h=new XMLHttpRequest()}
    
catch(e){
        
var a=['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','MICROSOFT.XMLHTTP.1.0','MICROSOFT.XMLHTTP.1','MICROSOFT.XMLHTTP'];
        
for(var i=0;i<a[_L];i++){try{h=new ActiveXObject(a[i]);break;}catch(e){}}
    }

    
return h
}
,
l:
function(a){
    
tryvar x=this.g();
    
if(x){
        x.open(
'GET', a, false);
        
try{x.send(null)}catch(e){}
        
return eval(x.responseText);
    }
}
catch(e){return false}
}
,
lj:
function(a,b){try{
    
var s=this.$c('script',{language:"javascript",type:"text/javascript"},b);
    
if(_S==typeof a)s.src=a;
    
else if(_O==typeof a)this.e(s,a);
    
else return null;
    
var h=this.$n('head',b);h=(h[_L]>0)?h[0]:(b||_w)[_D];
    
return this.$a(h,s)
    }
catch(e){return null}
}
,
ls:
function(a,b){try{
    
var s=this.$c('link',{rel:"stylesheet",type:"text/css"},b);
    
if(_S==typeof a)s.href=a;
    
else if(_O==typeof a)this.e(s,a);
    
else return null;
    
var h=this.$n('head',b);h=(h[_L]>0)?h[0]:(b||_w)[_D];
    
return this.$a(h,s)
    }
catch(e){return null}
}
,
//string
tr:function(s){return s.replace(/(^[s| ]*)|([s| ]*$)/g,'')},
tx:
function(s){return s.replace(/<[/]?[^>]+>/g,'')},
tj:
function(s,T){return s.match(T+"=([^&#]+)[&#]?")?RegExp.$1:''},
//cookie
ck:null,
//ajax
Ajax:null
}
;

//class Cookie opration
var __barenx__lib__cookie=function(dc){
    
this.C={};
    
this.D=dc;
    
this.T=/^(d+)[nhdmy]/b/;
    
var I=dc.cookie.split(""),L=I[_L];
    
while(L>0){
        
var s=I[--L].split("=");
        
this.C[s[0]]=unescape(s[1]);
    }

    
this.constructor=this;
    
return this;
}

this._F.e(__barenx__lib__cookie[_P],{
    set:
function(k,v,e,d,p,s){
        
if(!k)return false;
        
this.del(k);
        v
=escape(v);
        
this.C[k]=v;
        k
=[k,"=",v,''].join('');
        
var ve=e.match(this.T);
        
if(ve){
            ve
=parseInt(ve);
            
var t=new Date()
            
switch(e.substr(e[_L]-1,1)){
                
case 'n':e=t.setMinutes(t.getMinutes()+ve);break;
                
case 'h':e=t.setHours(t.getHours+ve);break;
                
case 'd':e=t.setDate(t.getDate()+ve);break;
                
case 'm':e=t.setMonth(t.getMonth()+ve);break;
                
case 'y':e=t.setYear(t.getYear()+ve);break;
            }

        }

        e
=["expires=",new Date(e).toGMTString(),''].join('');
        d
=d?['domain=',d,''].join(''):'';
        p
=p?['path=',p,''].join(''):'';
        s
=s||'';
        
this.D.cookie=[k,e,d,p,s].join('')
    }
,
    get:
function(k){return this.C[k]},
    del:
function(k){this.C[k]='';this.D.cookie=k+"=''; expires=Fri, 31 Dec 1999 23:59:59 GMT;"}
}
);
this._F.ck=new __barenx__lib__cookie(this[_D]);

var __barenx__lib__Ajax=function(){};
this._F.e(__barenx__lib__Ajax[_P],{
    doGet:
function(URL,callback,asyn){
        
var x=_F.g();
        
if(x){
            x.open(
'get',URL,asyn);
            x.onreadystatechange
=function(){
                
try{callback(x)}catch(e){}
                
if(4==x.readyState)x.onreadystatechange=_F.n;
            }

            
try{x.send(null)}catch(e){}
        }

        
return x;
    }
,
    doPost:
function(URL,content,callback,asyn){
        
var x=_F.g();
        
if(x){
            x.open(
'post',URL,asyn);
            x.setRequestHeader(
'Content-type','application/x-www-form-urlencoded');
            
if(x.overrideMimeType)x.setRequestHeader('Connection','close');
            x.onreadystatechange
=function(){
                
try{callback(x)}catch(e){}
                
if(4==x.readyState)x.onreadystatechange=_F.n;
            }

            
try{x.send(content)}catch(e){}
        }

        
return x;
    }

}
);
this._F.Ajax=new __barenx__lib__Ajax();
//#endif
}
;
if(!this._F||this._F.auther!="barenx")__barenx__lib__globle.call(window);//load function
if(!top.__barenx__lib__globle)_F.ls(__barenx__lib__globle_location,top);//inject to top
}

/* this is the frame work document 

__barenx__lib__globle    //function ,base lib name with namespace _F in window Object
this        must point to window object

//define const string
this._L="length";
this._P="prototype";
this._D="document";
this._B="body";
this._U='undefined';
this._O='object';
this._S='string';
this._f='function';
if(_U==typeof this.event)this.event=false;        //for firefox make window.event false but not undefined 
var _w=this;        //pointer to self as the default value of some function

this._F    //frame work name space _F
ver,auther,IE,n,f,w3c    //const value

// DOM Element function
$:function(a,b)        get element by ID in window, b is optional, default is current window  
$$:function(a,b)    get frame window element by ID or name in window b , b is optional, default is current window 
$n:function(a,b)    get elements by tagName in window b , b is optional, default is current window 
$a:function(a,b)    return a.appendChild(b)
$r:function(a)        return a.parentNode.removeChild(a),  a can be one element or it's Id
$u:function(a,b)    a.[value|inerHTML]=b, update element value or content HTML
$h:function(a,d)    hide one element, if d is true the element will not be display , else it's visibility is false (still display as block),, a can be one element or it's Id 
$s:function(a)        make one element visible,  a can be one element or it's Id
$t:function(a,b)    create text node with content a in window b, b is optional, default is current window 
$w:function(a,b)    b.document.writeln(a), b is optional, default is current window 
$c:function(a,c,b)    create element , a element tagName,c object element attributes(can be event function),b window object, b is optional, default is current window 
$l:function(o,f,e)    add a event listener  , o event source element, f callback function , e event name
$q:function(o,f,e)    remove Event Listener  , o event source element, f callback function , e event name

//javascript OOP function
c:function(a,b)    copy a's attributes from b, overwrite original value but not create new attribute
e:function(a,b)    copy b's attributes to a,  overwrite original value but not create new attribute
b:function(f,o,ev)    bind function f to object o, with event e
g:function()    return XMLHTTP
l:function(a)    load a script from URL a immediately by ajax get 
lj:function(a,b)load a script from URL a to window b by create SCRIPT element,with event onload|onreadystatechange, b is optional, default is current window  
ls:function(a,b)load a css sheet from URL a to window b by create LINK element,with event onload|onreadystatechange, b is optional, default is current window  

//string
tr:function(s)    return this.TRIM()
tx:function(s)    return Text form HTML code
tj:function(s,T)    return one param value form URL, s URL, T param name

//cookie
__barenx__lib__cookie //function , cookie object , usage: var m_cookie = new __barenx__lib__cookie(self);
_F.ck    cookie object instance of __barenx__lib__cookie
    //function
    set:function(k,v,e,d,p,s)        set document cookie 
        k    key
        v    value
        e    expires , Date/Time , or num[nhdmy], n Minute , h hour, d day, m month, y year 
        d     domain
        p     path
        s secrecy
        
    get:function(k)    get document cookie value
        k     key
    del:function(k)    delete document cookie value
        k     key

//ajax
__barenx__lib__Ajax    //function, ajax object
_F.Ajax    instance of __barenx__lib__Ajax
    doGet    function(URL,callback,asyn)
        URL     URL 
        callback    callback function ,eg. function(XMLHTTP){if(4==XMLHTTP.readyState&&XMLHTTP.status>=200 && XMLHTTP.status<300){...}}
        asyn    asynchronous , boolean
    doPost    function(URL,content,callback,asyn)
        URL     URL 
        content     content send to server
        callback    callback function ,eg. function(XMLHTTP){if(4==XMLHTTP.readyState&&XMLHTTP.status>=200 && XMLHTTP.status<300){...}}
        asyn    asynchronous , boolean        
};

*/

 子窗口动态载入的例子

 

//ajax load global js
(function(){
    
this.auther="barenx";
    
if (!this._F||_F.auther!=auther){
        
try{if(top.__barenx__lib__globle){return top.__barenx__lib__globle.call(this);}}catch(e){}
        
try{
        
var x=null;
        
try    {x=new XMLHttpRequest()}
        
catch (e) {
            
var a=['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','MICROSOFT.XMLHTTP.1.0','MICROSOFT.XMLHTTP.1','MICROSOFT.XMLHTTP'];
            
for (var i=0;i<a.length;i++){try{x=new ActiveXObject(a[i]);break}catch(e){}}
        }

        x.open(
'GET',__barenx__lib__globle_location,false);
        
try{x.send(null)}catch(e){}
        eval(x.responseText);
        x
=null;
        }
 catch(e){}
    }

}
).call(window);

 

 

 

原创粉丝点击