zoukankan      html  css  js  c++  java
  • common.js 2017

    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') }
            }
    
  • 相关阅读:
    【2020-08-15】人生十三信条
    【2020-08-14】何太生气的第三天
    【中间件】MyCat读写分离、分库分表
    sort()方法的用法,参数以及排序原理
    理解Object.defineProperty的作用
    VMware Workstation 11 中 Ubuntu 14.04 的 VMware Tools 问题 :没有显示共享文件夹
    VMware Workstation 11 中 Ubuntu 14.04 的 VMware Tools 问题 :没有显示共享文件夹
    如何提高编程能力?
    如何提高编程能力?
    Xshell用鼠标选中一段文字后自动换行的问题
  • 原文地址:https://www.cnblogs.com/sntetwt/p/6492051.html
Copyright © 2011-2022 走看看