常用js封装

获取url参数(直接取?后边的参数)

function getUrlQuerys() {
  const queryStr = location.search.length ? location.search.substring(1) : '';
  const queryArr = queryStr.length ? queryStr.split('&') : [];
  let querys = {};
  queryArr.forEach(item => {
    let temp = item.split('=');
    querys[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
  });
  return querys;
}
const urlQuery = getUrlQuerys();  
console.log(urlQuery['urlName'])

//获取url参

function getUrlParams(name, url) {

  if (!url) url = location.href;
  name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
  var regexS = '[\\?&]' + name + '=([^&#]*)';
  var regex = new RegExp(regexS);
  var results = regex.exec(url);
  return results == null ? null : results[1];
}

// 获取登录状态 & 用户昵称

function getMUserInfo() {
  let loggedIn = false; // 是否登录
  let uid = 0; // 用户ID

  const cookieClubUserShow = getCookie('getCookie');//要取的字段
  const autouserid = getCookie('getCookie');
  // const cookieClubUserShow = '52100122|197|2|%e7%9a%b1%e7%9c%89_|0|0|0|/g11/M04/AB/BA/120X120_0_q87_autohomecar__wKjBzFnkZg6ABBS_AAFIXhsHM1c188.jpg|2018-07-31 19:42:33|0'
  if (cookieClubUserShow && autouserid) {
    const clubUserShow = cookieClubUserShow.split('|');
    loggedIn = true;
    [uid] = clubUserShow;
  }
  return {
    loggedIn,
    uid,
  };
}

判断是否是app

function getAppInfo() {
  const ua = navigator.userAgent;
  const co = document.cookie;
  const ur = /auto_(iphone|android)(;|%3B|\/)(\d+\.\d+\.\d+)/i;
  const cr = /.*app_key=auto_(iphone|android)(.*)app_ver=(\d+\.\d+\.\d+)/i;
  const match = ua.match(ur) || co.match(cr);

  return {
    isApp: /autohomeapp/.test(ua) || ur.test(ua) || cr.test(co),
    appKey: match && match[1],
    appVer: match && match[3],
  };
}

取浏览器内核,返回各终端类型

function getBrowser() {
  const ua = navigator.userAgent
  return {
    trident: ua.indexOf('Trident') > -1, // IE内核
    presto: ua.indexOf('Presto') > -1, // opera内核
    webKit: ua.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
    gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1, // 火狐内核
    mobile: !!ua.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
    ios: !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
    android: ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1, // android终端
    iPhone: ua.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
    iPad: ua.indexOf('iPad') > -1, // 是否iPad
    webApp: ua.indexOf('Safari') === -1, // 是否web应该程序,没有头部与底部
    weixin: ua.indexOf('MicroMessenger') > -1, // 是否微信 (2015-01-22新增)
    qq: ua.match(/\sQQ/i) === ' qq', // 是否QQ
    escApp: ua.indexOf('UsedCar') > -1, // 二手车APP
    autohome: ua.indexOf('autohomeapp') > -1
  }
}

// 打开弹框,页面禁止滚动

var scrollDistence = 0; function noScroll() { scrollDistence = $(window).scrollTop(); $('body').css({ overflow: 'hidden', position: 'fixed', width: '100%', top: 0, }); } // 关闭弹框,页面正常显示 function canScroll() { $('body').css({ overflow: '', position: 'static', width: 'auto', top: 0, }); $(window).scrollTop(scrollDistence); }
 
posted @ 2020-02-20 16:39  duqianqian  阅读(305)  评论(0编辑  收藏  举报