分享别人写的一个小型js框架
人气:0
主要是集成了常用的扩展,包括Dom与String、Array、StringBuffer、NameSpace等,当然还少不了Ajax。体积约8k。因为严格控制体积,所以功能有限。如果只要Ajax部分,就1k而已。
这个小框架已经应用到公司的项目,并开始逐步在前端展示页面上接替prototype.js。以后将围绕这个框架做一些应用。
/*
* My JavaScript Framework
* Version : 1.0 beta
* Author : misshjn
* Email : misshjn@163.com
*/
function NameSpace(){};
function StringBuffer(){this.data = []};
var Method = {
Version : "1.0 beta",
Element : function(){
this.version = Method.Version;
this.hide = function(){this.style.display="none"; return this};
this.show = function(){this.style.display=""; return this};
this.getStyle = function(s){
var value = this.style[s.camelize()];
if (!value){
if (this.currentStyle) {
value = this.currentStyle[s.camelize()];
}else if (document.defaultView && document.defaultView.getComputedStyle) {
var css = document.defaultView.getComputedStyle(this, null);
value = css ? css.getPropertyValue(s) : null;
}
}
return value;
};
this.setStyle = function(s){
var sList = s.split(";");
for (var i=0,j; j=sList[i]; i++){
var k = j.split(":");
this.style[k[0].trim().camelize()] = k[1].trim();
}
return this;
};
this.toggle = function(){this.getStyle("display")=="none"?this.setStyle("display:"):this.setStyle("display:none"); return this};
this.hasClassName = function(c){return this.className.hasSubString(c," ")?true:false;};
this.addClassName = function(c){if(!this.hasClassName(c)){this.className+=" "+c};return this};
this.removeClassName = function(c){if(this.hasClassName(c)){this.className = (" "+this.className+" ").replace(" "+c+" "," ").trim(); return this}};
this.getElementsByClassName = function(c){return this.getElementsByAttribute("className",c)};
this.getElementsByAttribute = function(n,v){ //name,value;
var elems = this.getElementsByTagName("*");
var elemList = [];
for (var i=0,j; j=elems[i]; i++){
var att = j[n] || j.getAttribute(n);
if (att==v){
elemList.push(j);
}
}
return elemList;
};
this.parentIndex = function(p){
if (this==p){return 0}
for (var i=0,n=this; n=n.parentNode; i++){
if(n==p){return i;}
if(n==document.documentElement) return -1;
}
}
this.nextElement = function(){
var n = this;
for (var i=0,n; n = n.nextSibling; i++){
if(n.nodeType==1) return $(n);
}
return null;
};
this.previousElement = function(){
var n = this;
for (var i=0,n; n = n.previousSibling; i++){
if(n.nodeType==1) return $(n);
}
return null;
};
this.moveAhead = function(){
if (this.previousElement()){
this.parentNode.insertBefore(this,this.previousElement());
}
return this;
};
this.moveBack = function(){
var n = this.nextElement();
if (n){
this.parentNode.removeChild(n);
this.parentNode.insertBefore(n,this);
}
return this;
};
},
Array : function(){
this.indexOf = function(){
for (i=0; i<this.length; i++){
if (this[i]==arguments[0])
return i;
}
return -1;
};
this.lastIndexOf = function(){
for (i=this.length-1; i>=0; i--){
if (this[i]==arguments[0])
return i;
}
return -1;
};
this.each = function(fn){
for (var i=0,len=this.length; i<len; i++){
fn(this[i]);
}
return this;
};
},
String : function(){
this.trim = function(){
var _re,_argument = arguments[0] || " ";
typeof(_argument)=="string"?(_argument == " "?_re = /(^\s*)|(\s*$)/g : _re = new RegExp("(^"+_argument+"*)|("+_argument+"*$)","g")) : _re = _argument;
return this.replace(_re,"");
};
this.ltrim = function(){
var _re, _argument = arguments[0] || " ";
typeof(_argument)=="string"?(_argument == " "?_re = /(^\s*)/g : _re = new RegExp("(^"+_argument+"*)","g")) : _re = _argument;
return this.replace(_re,"");
};
this.rtrim = function(){
var _re, _argument = arguments[0] || " ";
typeof(_argument)=="string"?(_argument == " "?_re = /(\s*$)/g : _re = new RegExp("("+_argument+"*$)","g")) : _re = _argument;
return this.replace(_re,"");
};
this.concat = function(){
var s = new StringBuffer();
s.append(this);
for (var i=0,j; j=arguments[i]; i++){
s.append(typeof j=="object" ? j.join("") : j);
}
return s.toString();
};
this.stripTags = function(){
return this.replace(/<\/?[^>]+>/gi, '');
};
this.cint = function(){
return this.replace(/\D/g,"")-0;
};
this.camelize = function(){
return this.replace(/(-\S)/g,function($1){return $1.toUpperCase().substring(1,2)})
};
this.hasSubString = function(s,f){
if(!f) f="";
var v = (f+this+f).indexOf(f+s+f);
return v==-1?false:v;
};
this.toXMLString = function(){
var arr = arguments[0].split("&");
var str = new StringBuffer();
for (var i=0,len=arr.length; i<len; i++){
var item = arr[i].split("=");
str.append("<"+item[0]+"><![CDATA["+item[1]+"]]></"+item[0]+">");
}
return str.toString();
};
this.URLEncode = function(){ return encodeURIComponent(this)};
this.URLDecode = function(){ return decodeURIComponent(this)};
},
StringBuffer : function(){
this.append = function(){this.data.push(arguments[0]);return this};
this.toString = function(){return this.data.join(arguments[0]||"")};
this.length = function(){return this.data.length};
},
NameSpace : function(){
this.copyChild = this.appendChild = function(ns){
for (var key in ns){
this[key] = ns[key];
}
return this;
};
}
};
Method.Array.apply(Array.prototype);
Method.String.apply(String.prototype);
Method.StringBuffer.apply(StringBuffer.prototype);
Method.NameSpace.apply(NameSpace.prototype);
function $(){
var elem = typeof(arguments[0])=="string"?document.getElementById(arguments[0]):arguments[0];
if (!elem){return null}
if (elem["version"]){return elem}
if ( arguments[1] == undefined || arguments[1]==true ){Method.Element.apply(elem);}
return elem;
};
$(document);
var Ajax={
xmlhttp:function (){
try{
return new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
return new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
return new XMLHttpRequest();
}
}
}
};
Ajax.Request=function (){
if(arguments.length<2)return ;
var para = {asynchronous:true,method:"GET",parameters:""};
for (var key in arguments[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 (var ReqHeader in para["setRequestHeader"]){
_x.setRequestHeader(ReqHeader,para["setRequestHeader"][ReqHeader]);
}
_x.send(para["method"].toUpperCase()=="POST"?(para["postBody"]?para["postBody"]:para["parameters"]):null);
};
var Cookies = {
get : function(n){
var dc = "; "+document.cookie+"; ";
var coo = dc.indexOf("; "+n+"=");
if (coo!=-1){
var s = dc.substring(coo+n.length+3,dc.length);
return unescape(s.substring(0, s.indexOf("; ")));
}else{
return null;
}
},
set : function(name,value,expires){
var expDays = expires*24*60*60*1000;
var expDate = new Date();
expDate.setTime(expDate.getTime()+expDays);
var expString = expires ? "; expires="+expDate.toGMTString() : "";
var pathString = ";path=/";
document.cookie = name + "=" + escape(value) + expString + pathString;
},
del : function(n){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=this.get(n);
if(cval!=null) document.cookie= n + "="+cval+";expires="+exp.toGMTString();
}
}
function $A(list){
var arr = [];
for (var i=0,len=list.length; i<len; i++){
arr[i] = list[i];
}
return arr;
}
function $D(str){return str.URLDecode();}
function $E(str){return str.URLEncode();}
function $V(id){return $(id).value}
function request(paras){
var url = location.href;
var paraString = "&"+url.substring(url.indexOf("?")+1,url.length)+"&";
if (paraString.indexOf("&"+paras+"=")==-1){return ""};
paraString = paraString.substring(paraString.indexOf("&"+paras+"=")+paras.length+2,paraString.length);
return paraString.substring(0,paraString.indexOf("&"));
}
这个小框架已经应用到公司的项目,并开始逐步在前端展示页面上接替prototype.js。以后将围绕这个框架做一些应用。
复制代码 代码如下:
/*
* My JavaScript Framework
* Version : 1.0 beta
* Author : misshjn
* Email : misshjn@163.com
*/
function NameSpace(){};
function StringBuffer(){this.data = []};
var Method = {
Version : "1.0 beta",
Element : function(){
this.version = Method.Version;
this.hide = function(){this.style.display="none"; return this};
this.show = function(){this.style.display=""; return this};
this.getStyle = function(s){
var value = this.style[s.camelize()];
if (!value){
if (this.currentStyle) {
value = this.currentStyle[s.camelize()];
}else if (document.defaultView && document.defaultView.getComputedStyle) {
var css = document.defaultView.getComputedStyle(this, null);
value = css ? css.getPropertyValue(s) : null;
}
}
return value;
};
this.setStyle = function(s){
var sList = s.split(";");
for (var i=0,j; j=sList[i]; i++){
var k = j.split(":");
this.style[k[0].trim().camelize()] = k[1].trim();
}
return this;
};
this.toggle = function(){this.getStyle("display")=="none"?this.setStyle("display:"):this.setStyle("display:none"); return this};
this.hasClassName = function(c){return this.className.hasSubString(c," ")?true:false;};
this.addClassName = function(c){if(!this.hasClassName(c)){this.className+=" "+c};return this};
this.removeClassName = function(c){if(this.hasClassName(c)){this.className = (" "+this.className+" ").replace(" "+c+" "," ").trim(); return this}};
this.getElementsByClassName = function(c){return this.getElementsByAttribute("className",c)};
this.getElementsByAttribute = function(n,v){ //name,value;
var elems = this.getElementsByTagName("*");
var elemList = [];
for (var i=0,j; j=elems[i]; i++){
var att = j[n] || j.getAttribute(n);
if (att==v){
elemList.push(j);
}
}
return elemList;
};
this.parentIndex = function(p){
if (this==p){return 0}
for (var i=0,n=this; n=n.parentNode; i++){
if(n==p){return i;}
if(n==document.documentElement) return -1;
}
}
this.nextElement = function(){
var n = this;
for (var i=0,n; n = n.nextSibling; i++){
if(n.nodeType==1) return $(n);
}
return null;
};
this.previousElement = function(){
var n = this;
for (var i=0,n; n = n.previousSibling; i++){
if(n.nodeType==1) return $(n);
}
return null;
};
this.moveAhead = function(){
if (this.previousElement()){
this.parentNode.insertBefore(this,this.previousElement());
}
return this;
};
this.moveBack = function(){
var n = this.nextElement();
if (n){
this.parentNode.removeChild(n);
this.parentNode.insertBefore(n,this);
}
return this;
};
},
Array : function(){
this.indexOf = function(){
for (i=0; i<this.length; i++){
if (this[i]==arguments[0])
return i;
}
return -1;
};
this.lastIndexOf = function(){
for (i=this.length-1; i>=0; i--){
if (this[i]==arguments[0])
return i;
}
return -1;
};
this.each = function(fn){
for (var i=0,len=this.length; i<len; i++){
fn(this[i]);
}
return this;
};
},
String : function(){
this.trim = function(){
var _re,_argument = arguments[0] || " ";
typeof(_argument)=="string"?(_argument == " "?_re = /(^\s*)|(\s*$)/g : _re = new RegExp("(^"+_argument+"*)|("+_argument+"*$)","g")) : _re = _argument;
return this.replace(_re,"");
};
this.ltrim = function(){
var _re, _argument = arguments[0] || " ";
typeof(_argument)=="string"?(_argument == " "?_re = /(^\s*)/g : _re = new RegExp("(^"+_argument+"*)","g")) : _re = _argument;
return this.replace(_re,"");
};
this.rtrim = function(){
var _re, _argument = arguments[0] || " ";
typeof(_argument)=="string"?(_argument == " "?_re = /(\s*$)/g : _re = new RegExp("("+_argument+"*$)","g")) : _re = _argument;
return this.replace(_re,"");
};
this.concat = function(){
var s = new StringBuffer();
s.append(this);
for (var i=0,j; j=arguments[i]; i++){
s.append(typeof j=="object" ? j.join("") : j);
}
return s.toString();
};
this.stripTags = function(){
return this.replace(/<\/?[^>]+>/gi, '');
};
this.cint = function(){
return this.replace(/\D/g,"")-0;
};
this.camelize = function(){
return this.replace(/(-\S)/g,function($1){return $1.toUpperCase().substring(1,2)})
};
this.hasSubString = function(s,f){
if(!f) f="";
var v = (f+this+f).indexOf(f+s+f);
return v==-1?false:v;
};
this.toXMLString = function(){
var arr = arguments[0].split("&");
var str = new StringBuffer();
for (var i=0,len=arr.length; i<len; i++){
var item = arr[i].split("=");
str.append("<"+item[0]+"><![CDATA["+item[1]+"]]></"+item[0]+">");
}
return str.toString();
};
this.URLEncode = function(){ return encodeURIComponent(this)};
this.URLDecode = function(){ return decodeURIComponent(this)};
},
StringBuffer : function(){
this.append = function(){this.data.push(arguments[0]);return this};
this.toString = function(){return this.data.join(arguments[0]||"")};
this.length = function(){return this.data.length};
},
NameSpace : function(){
this.copyChild = this.appendChild = function(ns){
for (var key in ns){
this[key] = ns[key];
}
return this;
};
}
};
Method.Array.apply(Array.prototype);
Method.String.apply(String.prototype);
Method.StringBuffer.apply(StringBuffer.prototype);
Method.NameSpace.apply(NameSpace.prototype);
function $(){
var elem = typeof(arguments[0])=="string"?document.getElementById(arguments[0]):arguments[0];
if (!elem){return null}
if (elem["version"]){return elem}
if ( arguments[1] == undefined || arguments[1]==true ){Method.Element.apply(elem);}
return elem;
};
$(document);
var Ajax={
xmlhttp:function (){
try{
return new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
return new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
return new XMLHttpRequest();
}
}
}
};
Ajax.Request=function (){
if(arguments.length<2)return ;
var para = {asynchronous:true,method:"GET",parameters:""};
for (var key in arguments[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 (var ReqHeader in para["setRequestHeader"]){
_x.setRequestHeader(ReqHeader,para["setRequestHeader"][ReqHeader]);
}
_x.send(para["method"].toUpperCase()=="POST"?(para["postBody"]?para["postBody"]:para["parameters"]):null);
};
var Cookies = {
get : function(n){
var dc = "; "+document.cookie+"; ";
var coo = dc.indexOf("; "+n+"=");
if (coo!=-1){
var s = dc.substring(coo+n.length+3,dc.length);
return unescape(s.substring(0, s.indexOf("; ")));
}else{
return null;
}
},
set : function(name,value,expires){
var expDays = expires*24*60*60*1000;
var expDate = new Date();
expDate.setTime(expDate.getTime()+expDays);
var expString = expires ? "; expires="+expDate.toGMTString() : "";
var pathString = ";path=/";
document.cookie = name + "=" + escape(value) + expString + pathString;
},
del : function(n){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=this.get(n);
if(cval!=null) document.cookie= n + "="+cval+";expires="+exp.toGMTString();
}
}
function $A(list){
var arr = [];
for (var i=0,len=list.length; i<len; i++){
arr[i] = list[i];
}
return arr;
}
function $D(str){return str.URLDecode();}
function $E(str){return str.URLEncode();}
function $V(id){return $(id).value}
function request(paras){
var url = location.href;
var paraString = "&"+url.substring(url.indexOf("?")+1,url.length)+"&";
if (paraString.indexOf("&"+paras+"=")==-1){return ""};
paraString = paraString.substring(paraString.indexOf("&"+paras+"=")+paras.length+2,paraString.length);
return paraString.substring(0,paraString.indexOf("&"));
}
加载全部内容