主要是集成了常用的扩展,包括Dom与String、Array、StringBuffer、NameSpace等,当然还少不了Ajax。体积约8k。因为严格控制体积,所以功能有限。如果只要Ajax部分,就1k而已。
这个小框架已经应用到公司的项目,并开始逐步在前端展示页面上接替prototype.js。以后将围绕这个框架做一些应用。
复制代码 代码如下:
/*
*MyJavaScriptFramework
*Version:1.0beta
*Author:misshjn
*Email:misshjn@163.com
*/
functionNameSpace(){};
functionStringBuffer(){this.data=[]};
varMethod={
Version:"1.0beta",
Element:function(){
this.version=Method.Version;
this.hide=function(){this.style.display="none";returnthis};
this.show=function(){this.style.display="";returnthis};
this.getStyle=function(s){
varvalue=this.style[s.camelize()];
if(!value){
if(this.currentStyle){
value=this.currentStyle[s.camelize()];
}elseif(document.defaultView&&document.defaultView.getComputedStyle){
varcss=document.defaultView.getComputedStyle(this,null);
value=css?css.getPropertyValue(s):null;
}
}
returnvalue;
};
this.setStyle=function(s){
varsList=s.split(";");
for(vari=0,j;j=sList[i];i++){
vark=j.split(":");
this.style[k[0].trim().camelize()]=k[1].trim();
}
returnthis;
};
this.toggle=function(){this.getStyle("display")=="none"?this.setStyle("display:"):this.setStyle("display:none");returnthis};
this.hasClassName=function(c){returnthis.className.hasSubString(c,"")?true:false;};
this.addClassName=function(c){if(!this.hasClassName(c)){this.className+=""+c};returnthis};
this.removeClassName=function(c){if(this.hasClassName(c)){this.className=(""+this.className+"").replace(""+c+"","").trim();returnthis}};
this.getElementsByClassName=function(c){returnthis.getElementsByAttribute("className",c)};
this.getElementsByAttribute=function(n,v){//name,value;
varelems=this.getElementsByTagName("*");
varelemList=[];
for(vari=0,j;j=elems[i];i++){
varatt=j[n]||j.getAttribute(n);
if(att==v){
elemList.push(j);
}
}
returnelemList;
};
this.parentIndex=function(p){
if(this==p){return0}
for(vari=0,n=this;n=n.parentNode;i++){
if(n==p){returni;}
if(n==document.documentElement)return-1;
}
}
this.nextElement=function(){
varn=this;
for(vari=0,n;n=n.nextSibling;i++){
if(n.nodeType==1)return$(n);
}
returnnull;
};
this.previousElement=function(){
varn=this;
for(vari=0,n;n=n.previousSibling;i++){
if(n.nodeType==1)return$(n);
}
returnnull;
};
this.moveAhead=function(){
if(this.previousElement()){
this.parentNode.insertBefore(this,this.previousElement());
}
returnthis;
};
this.moveBack=function(){
varn=this.nextElement();
if(n){
this.parentNode.removeChild(n);
this.parentNode.insertBefore(n,this);
}
returnthis;
};
},
Array:function(){
this.indexOf=function(){
for(i=0;i<this.length;i++){
if(this[i]==arguments[0])
returni;
}
return-1;
};
this.lastIndexOf=function(){
for(i=this.length-1;i>=0;i--){
if(this[i]==arguments[0])
returni;
}
return-1;
};
this.each=function(fn){
for(vari=0,len=this.length;i<len;i++){
fn(this[i]);
}
returnthis;
};
},
String:function(){
this.trim=function(){
var_re,_argument=arguments[0]||"";
typeof(_argument)=="string"?(_argument==""?_re=/(^s*)|(s*$)/g:_re=newRegExp("(^"+_argument+"*)|("+_argument+"*$)","g")):_re=_argument;
returnthis.replace(_re,"");
};
this.ltrim=function(){
var_re,_argument=arguments[0]||"";
typeof(_argument)=="string"?(_argument==""?_re=/(^s*)/g:_re=newRegExp("(^"+_argument+"*)","g")):_re=_argument;
returnthis.replace(_re,"");
};
this.rtrim=function(){
var_re,_argument=arguments[0]||"";
typeof(_argument)=="string"?(_argument==""?_re=/(s*$)/g:_re=newRegExp("("+_argument+"*$)","g")):_re=_argument;
returnthis.replace(_re,"");
};
this.concat=function(){
vars=newStringBuffer();
s.append(this);
for(vari=0,j;j=arguments[i];i++){
s.append(typeofj=="object"?j.join(""):j);
}
returns.toString();
};
this.stripTags=function(){
returnthis.replace(/</?[^>]+>/gi,'');
};
this.cint=function(){
returnthis.replace(/D/g,"")-0;
};
this.camelize=function(){
returnthis.replace(/(-S)/g,function($1){return$1.toUpperCase().substring(1,2)})
};
this.hasSubString=function(s,f){
if(!f)f="";
varv=(f+this+f).indexOf(f+s+f);
returnv==-1?false:v;
};
this.toXMLString=function(){
vararr=arguments[0].split("&");
varstr=newStringBuffer();
for(vari=0,len=arr.length;i<len;i++){
varitem=arr[i].split("=");
str.append("<"+item[0]+"><![CDATA["+item[1]+"]]></"+item[0]+">");
}
returnstr.toString();
};
this.URLEncode=function(){returnencodeURIComponent(this)};
this.URLDecode=function(){returndecodeURIComponent(this)};
},
StringBuffer:function(){
this.append=function(){this.data.push(arguments[0]);returnthis};
this.toString=function(){returnthis.data.join(arguments[0]||"")};
this.length=function(){returnthis.data.length};
},
NameSpace:function(){
this.copyChild=this.appendChild=function(ns){
for(varkeyinns){
this[key]=ns[key];
}
returnthis;
};
}
};
Method.Array.apply(Array.prototype);
Method.String.apply(String.prototype);
Method.StringBuffer.apply(StringBuffer.prototype);
Method.NameSpace.apply(NameSpace.prototype);
function$(){
varelem=typeof(arguments[0])=="string"?document.getElementById(arguments[0]):arguments[0];
if(!elem){returnnull}
if(elem["version"]){returnelem}
if(arguments[1]==undefined||arguments[1]==true){Method.Element.apply(elem);}
returnelem;
};
$(document);
varAjax={
xmlhttp:function(){
try{
returnnewActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
returnnewActiveXObject('Microsoft.XMLHTTP');
}catch(e){
returnnewXMLHttpRequest();
}
}
}
};
Ajax.Request=function(){
if(arguments.length<2)return;
varpara={asynchronous:true,method:"GET",parameters:""};
for(varkeyinarguments[1]){
para[key]=arguments[1][key];
}
var_x=Ajax.xmlhttp();
var_url=arguments[0];
if(para["parameters"].length>0)para["parameters"]+='&_=';
if(para["method"].toUpperCase()=="GET")_url+=(_url.match(/?/)?'&':'?')+para["parameters"];
_x.open(para["method"].toUpperCase(),_url,para["asynchronous"]);
_x.onreadystatechange=function(){
if(_x.readyState==4){
if(_x.status==200)
para["onComplete"]?para["onComplete"](_x):"";
else{
para["onError"]?para["onError"](_x):"";
}
}
};
if(para["method"].toUpperCase()=="POST")_x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
for(varReqHeaderinpara["setRequestHeader"]){
_x.setRequestHeader(ReqHeader,para["setRequestHeader"][ReqHeader]);
}
_x.send(para["method"].toUpperCase()=="POST"?(para["postBody"]?para["postBody"]:para["parameters"]):null);
};
varCookies={
get:function(n){
vardc=";"+document.cookie+";";
varcoo=dc.indexOf(";"+n+"=");
if(coo!=-1){
vars=dc.substring(coo+n.length+3,dc.length);
returnunescape(s.substring(0,s.indexOf(";")));
}else{
returnnull;
}
},
set:function(name,value,expires){
varexpDays=expires*24*60*60*1000;
varexpDate=newDate();
expDate.setTime(expDate.getTime()+expDays);
varexpString=expires?";expires="+expDate.toGMTString():"";
varpathString=";path=/";
document.cookie=name+"="+escape(value)+expString+pathString;
},
del:function(n){
varexp=newDate();
exp.setTime(exp.getTime()-1);
varcval=this.get(n);
if(cval!=null)document.cookie=n+"="+cval+";expires="+exp.toGMTString();
}
}
function$A(list){
vararr=[];
for(vari=0,len=list.length;i<len;i++){
arr[i]=list[i];
}
returnarr;
}
function$D(str){returnstr.URLDecode();}
function$E(str){returnstr.URLEncode();}
function$V(id){return$(id).value}
functionrequest(paras){
varurl=location.href;
varparaString="&"+url.substring(url.indexOf("?")+1,url.length)+"&";
if(paraString.indexOf("&"+paras+"=")==-1){return""};
paraString=paraString.substring(paraString.indexOf("&"+paras+"=")+paras.length+2,paraString.length);
returnparaString.substring(0,paraString.indexOf("&"));
}