common.js 2017

來源:互聯網
上載者:User

標籤:eval   utc   logs   gettime   gets   oid   ipad   加法   buffer   

String.IsNullOrEmpty = function (v) {            return !(typeof (v) === "string" && v.length != 0);        };        String.prototype.Trim = function (isall) {            if (isall) {                //清除所有空格                return this.replace(/\s/g, "");            }            //清除兩邊空格            return this.replace(/^\s+|\s+$/g, "")        };        //清除開始空格        String.prototype.TrimStart = function (v) {            if ($String.IsNullOrEmpty(v)) {                v = "\\s";            };            var re = new RegExp("^" + v + "*", "ig");            return this.replace(re, "");        };        //清除結束空格        String.prototype.TrimEnd = function (v) {            if ($String.IsNullOrEmpty(c)) {                c = "\\s";            };            var re = new RegExp(c + "*$", "ig");            return v.replace(re, "");        };        //擷取url參數,調用:window.location.search.Request("test"),如果不存在,則返回null        String.prototype.Request = function (para) {            var reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");            var r = this.substr(this.indexOf("\?") + 1).match(reg);            if (r != null) return unescape(r[2]); return null;        }        //四捨五入保留二位小數          Number.prototype.ToDecimal = function (dec) {            //如果是整數,則返回            var num = this.toString();            var idx = num.indexOf(".");            if (idx < 0) return this;            var n = num.length - idx - 1;            //如果是小數,則返回保留小數            if (dec < n) {                var e = Math.pow(10, dec);                return Math.round(this * e) / e;            } else {                return this;            }        }        //字元轉換為數字        String.prototype.ToNumber = function (fix) {            //如果不為數字,則返回0            if (!/^(-)?\d+(\.\d+)?$/.test(this)) {                return 0;            } else {                if (typeof (fix) != "undefined") { return parseFloat(this).toDecimal(fix); }                return parseFloat(this);            }        }        //Number類型加法toAdd        Number.prototype.ToAdd = function () {            var _this = this;            var len = arguments.length;            if (len == 0) { return _this; }            for (i = 0 ; i < len; i++) {                var arg = arguments[i].toString().toNumber();                _this += arg;            }            return _this.toDecimal(2);        }        //Number類型減法toSub        Number.prototype.ToSub = function () {            var _this = this;            var len = arguments.length;            if (len == 0) { return _this; }            for (i = 0 ; i < len; i++) {                var arg = arguments[i].toString().toNumber();                _this -= arg;            }            return _this.toDecimal(2);        }        //字元格式設定化:String.format("S{0}T{1}","n","e");//結果:SnTe        String.Format = function () {            var c = arguments[0];            for (var a = 0; a < arguments.length - 1; a++) {                var b = new RegExp("\\{" + a + "\\}", "gm");                c = c.replace(b, arguments[a + 1])            }            return c        };        /*        *輸入鍵台類(長度小於,輸入鍵台)        *調用執行個體        *var s = "471812366";        *s.leftpad(10, ‘00‘);    //結果:00471812366        *s.rightpad(10, ‘00‘);   //結果:47181236600        *左填充        */        String.prototype.LeftPad = function (b, f) {            if (arguments.length == 1) {                f = "0"            }            var e = new StringBuffer();            for (var d = 0,            a = b - this.length; d < a; d++) {                e.append(f)            }            e.append(this);            return e.toString()        };        //右填充        String.prototype.RightPad = function (b, f) {            if (arguments.length == 1) {                f = "0"            }            var e = new StringBuffer();            e.append(this);            for (var d = 0,            a = b - this.length; d < a; d++) {                e.append(f)            }            return e.toString()        };        //載入JS檔案        //調用:window.using(‘/scripts/test.js‘);        window.using = function (jsPath, callback) {            $.getScript(jsPath, function () {                if (typeof callback == "function") {                    callback();                }            });        }        //自訂命名空間        //定義:namespace("Utils")["Test"] = {}        //調用:if (Utils.Error.hasOwnProperty(‘test‘)) { Utils.Error[‘test‘](); }        window.namespace = function (a) {            var ro = window;            if (!(typeof (a) === "string" && a.length != 0)) {                return ro;            }            var co = ro;            var nsp = a.split(".");            for (var i = 0; i < nsp.length; i++) {                var cp = nsp[i];                if (!ro[cp]) {                    ro[cp] = {};                };                co = ro = ro[cp];            };            return co;        };        /*====================================        建立Cookie:Micro.Cookie("名稱", "值", { expires: 7 }, path: ‘/‘ );        expires:如果省略,將在使用者退出瀏覽器時被刪除。         path:的預設值為網頁所擁有的網域名稱        添加Cookie:Micro.Cookie("名稱", "值");        設定有效時間(天):Micro.Cookie("名稱", "值", { expires: 7 });        設定有效路徑(天):Micro.Cookie("名稱", "值", { expires: 7 }, path: ‘/‘ );        讀取Cookie: Micro.Cookie("名稱"});,如果cookie不存在則返回null         刪除Cookie:Micro.Cookie("名稱", null); ,刪除用null作為cookie的值即可        *楊秀徐        ====================================*/        namespace("Micro")["Cookie"] = function (name, value, options) {            if (typeof value != ‘undefined‘) {                options = options || {};                if (value === null) {                    value = ‘‘;                    options.expires = -1;                }                var expires = ‘‘;                if (options.expires && (typeof options.expires == ‘number‘ || options.expires.toUTCString)) {                    var date;                    if (typeof options.expires == ‘number‘) {                        date = new Date();                        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));                    } else {                        date = options.expires;                    }                    expires = ‘; expires=‘ + date.toUTCString();                }                var path = options.path ? ‘; path=‘ + (options.path) : ‘‘;                var domain = options.domain ? ‘; domain=‘ + (options.domain) : ‘‘;                var secure = options.secure ? ‘; secure‘ : ‘‘;                document.cookie = [name, ‘=‘, encodeURIComponent(value), expires, path, domain, secure].join(‘‘);            } else {                var cookieValue = null;                if (document.cookie && document.cookie != ‘‘) {                    var cookies = document.cookie.split(‘;‘);                    for (var i = 0; i < cookies.length; i++) {                        var cookie = cookies[i].Trim(true);                        if (cookie.substring(0, name.length + 1) == (name + ‘=‘)) {                            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));                            break;                        }                    }                }                return cookieValue;            }        };                //錯誤處理方法        namespace("Micro")["Error"] = {            a: function () { alert(‘a‘) },            b: function () { alert(‘b‘) },            c: function () { alert(‘c‘) }        }        //常規處理方法        namespace("Micro")["Utils"] = {            isMobile: function () {                var userAgent = navigator.userAgent.toLowerCase();                var isIpad = userAgent.match(/ipad/i) == "ipad";                var isIphoneOs = userAgent.match(/iphone os/i) == "iphone os";                var isMidp = userAgent.match(/midp/i) == "midp";                var isUc7 = userAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";                var isUc = userAgent.match(/ucweb/i) == "ucweb";                var isAndroid = userAgent.match(/android/i) == "android";                var isCE = userAgent.match(/windows ce/i) == "windows ce";                var isWM = userAgent.match(/windows mobile/i) == "windows mobile";                if (isIpad || isIphoneOs || isMidp || isUc7 || isUc || isAndroid || isCE || isWM) {                    return true;                } else {                    return false;                }            },            b: function () { alert(‘b‘) },            c: function () { alert(‘c‘) }        }

 

common.js 2017

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.