It mainly integrates common extensions, including Dom and String, Array, StringBuffer, and NameSpace. Of course Ajax is indispensable. The size is about 8 KB. Because the volume is strictly controlled, the function is limited. If you only need the Ajax part, it will be 1 K.
This small framework has been applied to the company's projects and began to gradually replace prototype. js on the front-end display page. We will make some applications around this framework in the future.
Copy codeThe Code is as follows :/*
* 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.doc umentElement) 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. previuselement = function (){
Var n = this;
For (var I = 0, n; n = n. previussibling; I ++ ){
If (n. nodeType = 1) return $ (n );
}
Return null;
};
This. moveAhead = function (){
If (this. previuselement ()){
This. parentNode. insertBefore (this, this. previuselement ());
}
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 ("&"));
}