前端实用小工具(URL参数截取、JSON判断、数据类型检测、版本号对比等)

 URL截取参数

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于网站建设、成都网站建设、虹口网络推广、微信平台小程序开发、虹口网络营销、虹口企业策划、虹口品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供虹口建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

 
 
 
 
  1. //直接调用输入想要截取的参数名称几个 
  2. export function getParamFromUrl(key) { 
  3.     if (key === undefined) return null; 
  4.     let search = location.search.substr(1); 
  5.     let mReg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)'); 
  6.     let mValue = search.match(mReg); 
  7.     if (mValue != null) return unescape(mValue[2]); 
  8.     return null; 
  9. //示例 
  10. let city = getParamFromUrl('city'); 

JSON是否为空判断

 
 
 
 
  1. //输入想要检测的json数据 如果为空返回返回false 
  2. export function isNullObject(model) { 
  3.   if (typeof model === "object") { 
  4.     let hasProp = false; 
  5.     for (const prop in model) { 
  6.         hasProp = true; 
  7.         break; 
  8.     } 
  9.     if (hasProp) { 
  10.         return false; 
  11.     } 
  12.     return true; 
  13.   } else { 
  14.       throw "model is not object"; 
  15.   } 

数据类型检测

 
 
 
 
  1. //检测变量的数据类型 
  2. export function getParamType(item) { 
  3.     if (item === null) return null; 
  4.     if (item === undefined) return undefined; 
  5.     return Object.prototype.toString.call(item).slice(8, -1); 
  6. //返回String Function Boolean Object Number 

获取cookie

 
 
 
 
  1. //获取document下cookie的具体某个参数值 
  2. export function getCookie(key) { 
  3.     if (key === undefined) { 
  4.         return undefined; 
  5.     } 
  6.     let cookies = document.cookie; 
  7.     let mReg = new RegExp('(^|;)\\s*' + key + '=([^;]*)(;|$)'); 
  8.     let mValue = cookies.match(mReg); 
  9.     let ret = undefined; 
  10.     if (mValue != null) { 
  11.         ret = unescape(mValue[2]); 
  12.     } 
  13.     if (ret !== undefined) { 
  14.         ret = ret.replace(/^\"|\'/i, '').replace(/\"|\'$/i, ''); 
  15.     } 
  16.     return ret; 

版本号对比
一般在做APP端开发的时候需要用到一些版本控制,那么就需要针对版本号来进行对比,高版本或者低版本做一些特殊的逻辑处理,下面就是提供版本对比的方法

 
 
 
 
  1. //传入要对比的版本号,一般前面一个传入当前的版本号,后面一个写上要对比的版本号 
  2. export function versionCompare(higher, lower) { 
  3.     let sep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.'; 
  4.  
  5.     let higherAry = higher.split(sep), 
  6.         lowerAry = lower.split(sep); 
  7.     let l = Math.max(higherAry.length, lowerAry.length); 
  8.     for (let i = 0; i < l; i++) { 
  9.         let high = parseInt(higherAry[i] || 0); 
  10.         let low = parseInt(lowerAry[i] || 0); 
  11.         if (high > low) { 
  12.             return 1; 
  13.         } 
  14.         if (high < low) { 
  15.             return -1; 
  16.         } 
  17.     } 
  18.     return 0; 
  19. //返回值  higher > lower: 1;higher = lower: 0;higher < lower:-1 

数组去重

 
 
 
 
  1. export function arrayUniq(array){ 
  2.     let temp = [];  
  3.     for(var i = 0; i < array.length; i++){ 
  4.         if(temp.indexOf(array[i]) == -1){ 
  5.             temp.push(array[i]); 
  6.         } 
  7.     } 
  8.     return temp; 

iPhone X系列机型判断

 
 
 
 
  1. export function isIphoneX() { 
  2.     // iPhone X、iPhone XS 
  3.     var isIPhoneX = 
  4.         /iphone/gi.test(window.navigator.userAgent) && 
  5.         window.devicePixelRatio && 
  6.         window.devicePixelRatio === 3 && 
  7.         window.screen.width === 375 && 
  8.         window.screen.height === 812; 
  9.     // iPhone XS Max 
  10.     var isIPhoneXSMax = 
  11.         /iphone/gi.test(window.navigator.userAgent) && 
  12.         window.devicePixelRatio && 
  13.         window.devicePixelRatio === 3 && 
  14.         window.screen.width === 414 && 
  15.         window.screen.height === 896; 
  16.     // iPhone XR 
  17.     var isIPhoneXR = 
  18.         /iphone/gi.test(window.navigator.userAgent) && 
  19.         window.devicePixelRatio && 
  20.         window.devicePixelRatio === 2 && 
  21.         window.screen.width === 414 && 
  22.         window.screen.height === 896; 
  23.     if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) { 
  24.         return true; 
  25.     } 
  26.     return false; 

分享文章:前端实用小工具(URL参数截取、JSON判断、数据类型检测、版本号对比等)
新闻来源:http://www.hantingmc.com/qtweb/news3/480903.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联