JavaScript several common static methods

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. Cookies), TMP;            if (TMP=RS) return unescape (tmp[2]);        return null; }, Delcookie:function (name) {Document.cookie = name + "=-1" + "; Expires=fri, Dec 1999 23:59:59 GMT; ";},/** *url string *parms string *method Strin G 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: "") + "&amp ; t= "+new Date (). GetTime (), method=config.method| |"            Get ", asy=true;            var http_request=null; if (method.tolowercase () = = "Get") {url=url+ "?"                +parms;            Parms=null; }//Begins 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, create object instance failed throw new Error (                "Cannot create an 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 = =) {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-url            Encoded ");        } http_request.send (parms); }    };} ();

  

JavaScript several common static methods

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.