JS Common tool functions

Source: Internet
Author: User

/** * Created by GAOJUN-PD on 2016/10/27. */var Util = {/** * 1, Judge non-null * 2, get string True length Chinese characters two bits * 3, determine parameter type * 4, date format * 5, get the parameter value in URL by key * 6, set Cookie value * 7, get cookie value * 8, Delete cookie * 9, HTML code * 10, HTML decode * 11, the cursor stops behind the text, the text box gets focus when called * 12, generates a new GUI  D *//** * Determine non-null * @param obj * @returns {boolean} */Isempty:function (obj) {if (obj = = Undefined | | obj = = NULL | |        New String (obj). Trim () = = ') {return true;        } else {return false;        }},/** * Get string True length Chinese characters are counted two bits * @param str * @returns {Number} */Getreallength:function (str) { return IsEmpty (str)?    0:str.replace (/[^\x00-\xff]/g, "* *"). Length; },/** * Determines the parameter type * @param obj * @returns {string} */Type:function (obj) {var class2type = {        }, toString = Object.prototype.toString; (function () {var Typearr = "Boolean,number,string,function,array,dAte,regexp,object ". Split (", ");                for (var i = 0; i < typearr.length; i++) {var name = Typearr[i];            class2type["[Object" + name + "]"] = Name.tolowercase (); }}) () return obj = = null? String (obj): Class2type[tostring.call (obj)] | |    "Object";    },/** * date formatting * @param a Date Date Object * @param formatstr formatted string such as Yyyy-mm-dd Hh:mm:ss * @returns {*} * *        Dateformat:function (date, formatstr) {var str = FORMATSTR;        var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; str = str.replace (/yyyy|        yyyy/, This.getfullyear ()); str = str.replace (/yy| yy/, (this.getyear ()%) > 9?        (This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100)); str = str.replace (/mm/, (This.getmonth () + 1) > 9?        (This.getmonth () + 1). ToString (): ' 0 ' + (this.getmonth () + 1));        str = str.replace (/m/g, (This.getmonth () + 1)); str = str.replace (/w|        W/g, Week[this.getday ()]); StR = str.replace (/dd| Dd/, This.getdate () > 9?        This.getdate (). toString (): ' 0 ' + this.getdate ()); str = str.replace (/d|        D/g, This.getdate ()); str = str.replace (/hh| hh/, This.gethours () > 9?        This.gethours (). toString (): ' 0 ' + this.gethours ()); str = str.replace (/h|        H/g, This.gethours ());        str = str.replace (/mm/, This.getminutes () > 9? this.getminutes (). toString (): ' 0 ' + this.getminutes ());        str = str.replace (/m/g, This.getminutes ()); str = str.replace (/ss| ss/, This.getseconds () > 9?        This.getseconds (). toString (): ' 0 ' + this.getseconds ()); str = str.replace (/s|        S/g, This.getseconds ()); Return str},/** * Gets the parameter value in the URL by key * @param key * @returns {NULL} */Getquerystring:function (k        EY) {var reg = new RegExp ("(^|&)" + key + "= ([^&]*) (&|$)", "I");        var r = window.location.search.substr (1). Match (REG);        if (r! = null) return decodeURIComponent (r[2]);  return null;  },/** * Set cookie value * @param name name * @param value name corresponds to values * @param Hours Expiration Time */Setcookie:fun        Ction (name, value, Hours) {var d = new Date ();        var offset = 8;        var UTC = D.gettime () + (D.gettimezoneoffset () * 60000);        var nd = UTC + (3600000 * offset);        var exp = new Date (ND);        Exp.settime (Exp.gettime () + Hours * 60 * 60 * 1000);    Document.cookie = name + "=" + encodeuricomponent (value) + ";p ath=/;expires=" + exp.togmtstring () + ";d omain=sicd.com;"; },/** * Get cookie Value * @param name Cookie name * @returns {*} */getcookie:function (name) {VA R arr = document.cookie. Match (New RegExp ("(^|)" + name + "= ([^;]        *)(;|$)"));        if (arr! = null) return decodeURIComponent (arr[2]);    return null;         },/** * Delete cookie * @param name Cookie name */delcookie:function (name) {var exp = new Date ();        Exp.settime (Exp.gettime ()-1); VAr cval = GetCookie (name);    if (cval! = null) Document.cookie = name + "=" + Cval + "; expires=" + exp.togmtstring (); },/** * HTML encoding * @param str to encode String * @returns {string} */Html_encode:function (str) {var        s = "";        if (Str.length = = 0) return "";        s = str.replace (/&/g, ">");        s = S.replace (/</g, "<");        s = S.replace (/>/g, ">");        s = s.replace (//g, " ");        s = s.replace (/\ '/g, "'");        s = s.replace (/\ "/g," "" ");        s = S.replace (/\n/g, "<br>");    return s;  },/** * HTML decoding * @param str string to decode * @returns {string} */Html_decode:function (str) {var        s = "";        if (Str.length = = 0) return "";        s = str.replace (/>/g, "&");        s = S.replace (/</g, "<");        s = S.replace (/>/g, ">");        s = S.replace (/ /g, "");        s = s.replace (/'/g, "\ '");        s = s.replace (/"/g," \ ""); S= S.replace (/<br>/g, "\ n");    return s;        },/** * Cursor stops after the text, the text box gets focus when called */Focuslast:function () {var e = event.srcelement;        var r = E.createtextrange ();        R.movestart (' character ', e.value.length);        R.collapse (TRUE);    R.select (); },/** * generates a new GUID * @return {string} data type * @method nuID */nuid:function () {return new    Date (). GetTime (). toString (36); }}

  

JS Common tool functions

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.