Share a common javascript static class and a javascript static class
If you don't talk much about it, simply provide the Code. If you know what to do with it, take it directly.
Copy codeThe Code is as follows:
Util = function (){
Return {
$: Function (id ){
Return document. getElementById (id );
},
Trim: function (str ){
Return str. replace (/(^ \ s +) | (\ s + $)/g ,"");
},
Len: function (str ){
Return str. replace (/[^ \ x00-\ xff]/g, '**'). length;
},
Format: function (str ){
Var arg = arguments;
Return str. replace (/\ {(\ d +) \}/g, function (m, I ){
Return arg [parseInt (I) + 1];
});
},
Each: function (object, callback, args ){
Var name, I = 0, length = object. length;
If (args ){
If (length = undefined ){
For (name in object)
If (callback. apply (object [name], args) === false)
Break;
} Else
For (; I <length ;)
If (callback. apply (object [I ++], args) === false)
Break;
} Else {
If (length = undefined ){
For (name in object)
If (callback. call (object [name], name, object [name]) === false)
Break;
} Else
For (var value = object [0];
I <length & callback. call (value, I, value )! = False; value = object [++ I]) {}
}
},
SetCookie: function (name, value, hours, path, domain ){
Var str = new String ();
Var nextTime = new Date ();
NextTime. setHours (nextTime. getHours () + hours );
Str = name + "=" + escape (value );
If (hours)
Str + = "; expires =" + nextTime. toGMTString ();
If (path)
Str + = "; path =" + path;
If (domain)
Str + = "; domain =" + domain;
Document. cookie = str;
},
GetCookie: function (name ){
Var rs = new RegExp ("(^ |)" + name + "= ([^;] *) (; | $)", "gi" cmd.exe c (document. cookie), tmp;
If (tmp = rs)
Return unescape (tmp [2]);
Return null;
},
DelCookie: function (name ){
Document. cookie = name + "=-1" + "; expires = Fri, 31 Dec 1999 23:59:59 GMT ;";
},
/**
* Url String
* Parms String
* Method String default value "get"
* Asy Boolean defalut value true
* Success Function (http_request.responseText)
**/
Ajax: function (config ){
Var url = config. url,
Parms = (config. parms? Config. parms: "") + "& t =" + new Date (). getTime (),
Method = config. method | "get ",
Asy = true;
Var http_request = null;
If (method. toLowerCase () = "get "){
Url = url + "? "+ Parms;
Parms = null;
}
// Start initializing the XMLHttpRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType) {// sets the MiME category
Http_request.overrideMimeType ("text/xml ");
}
} Else if (window. ActiveXObject) {// IE browser
Try {
Http_request = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Http_request = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Http_request) {// exception. An error occurred while creating the object instance.
Throw new Error ("the XMLHttpRequest object instance cannot be created .");
Return null;
}
Http_request.open (method, url, asy );
Http_request.onreadystatechange = function (){
If (http_request.readyState = 4 ){
Try {
If (http_request.status = 200 ){
Config. success (http_request.responseText );
}
} Catch (e ){
Throw new Error ("Data Reading failed .");
}
}
};
If (method. toLowerCase () = "post "){
Http_request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
}
Http_request.send (parms );
}
};
}();
Is it pretty good? I'm very satisfied.