码迷,mamicode.com
首页 > Web开发 > 详细

常用js封装

时间:2020-02-20 17:01:25      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:navigator   ==   lang   web   adr   获取   turn   home   ppi   

//获取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
  }
}

常用js封装

标签:navigator   ==   lang   web   adr   获取   turn   home   ppi   

原文地址:https://www.cnblogs.com/restart77/p/12336340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!