1、检查是否为微信浏览器
function isWxBrowser() { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i) == "micromessenger") { return true; } else { return false; } }
2、随机数时间戳
function uniqueId() { var a = Math.random, b = parseInt; return Number(new Date()).toString() + b(10 * a()) + b(10 * a()) + b(10 * a()); }
3、手机号码
function checkMobile(str) { if (!(/^1[3|5|8][0-9]\d{4,8}$/.test(str))) { return false; } return true; }
4、获取域名主机 params: url:域名
function getHost(url) { var host = "null"; if (typeof url == "undefined" || null == url) { url = window.location.href; } var regex = /^\w+\:\/\/([^\/]*).*/; var match = url.match(regex); if (typeof match != "undefined" && null != match) { host = match[1]; } return host; }
5、判断是否移动设备访问
function isMobileUserAgent() { return (/iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i.test(window.navigator.userAgent.toLowerCase())); }
6、判断是分别是安卓还是苹果设备访问
function isAppleMobileDevice() { // 苹果 return (/iphone|ipod|ipad|Macintosh/i.test(navigator.userAgent .toLowerCase())); } function isAndroidMobileDevice() { // 安卓 return (/android/i.test(navigator.userAgent.toLowerCase())); }
7、时间格式化
Date.prototype.Format = function(fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } //使用方法:(时间转成yyyy-MM-dd hh:mm:ss) var time1 = new Date().Format("yyyy-MM-dd"); console.log(time1,'time1') var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss"); console.log(time2,'time2')
8、数组去重 和 字符串里面的重复字符
// 去除数组的重复成员 [...new Set(arr)] //去除字符串里面的重复字符 [...new Set('aaabbbc')].join('')
9、生成随机数
function randombetween(min, max){ return min + (Math.random() * (max-min +1)); }