I write JavaScript common static method class, share to Everyone

Source: Internet
Author: User

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+= ";p ath=" +path;
if (domain)
str+= ";d omain=" +domain;
DOCUMENT.COOKIE=STR;
},
Getcookie:function (name) {
var rs=new RegExp ("(^|)" +name+ "= ([^;] *) (; |$) "," GI "). EXEC (Document.cookie), TMP;
if (TMP=RS)
Return unescape (tmp[2]);
return null;
},
Delcookie:function (name) {
Document.cookie = name + "=-1" + "; Expires=fri, to 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) {//Set 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, creation of object instance failed
throw new Error ("Cannot create XMLHttpRequest object instance.");
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 read failed.");
}
}
};
if (method.tolowercase () = = "Post") {
Http_request.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
}
Http_request.send (parms);
}
};
}();

I write JavaScript common static method class, share to Everyone

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.