首页 > 代码库 > common.js 2017
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 =http://www.mamicode.com/== null) {"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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。