• 如何判断当前网页环境是微信环境?


    可以通过这个获取 window.navigator.userAgent;

    //判断H5页面是否在微信/企业微信环境中打开:
    var ua = navigator.userAgent.toLowerCase(); // 将用户代理头的值转为小写

     

    //判断微信的方法的两种方法:
    ua.match(/micromessenger/i) == ‘micromessenger’
    /micromessenger/i.test(navigator.userAgent); //结果为true或者false

      

    //判断企业微信环境的两种方法:
    ua.match(/wxwork/i) == ‘wxwork’
    /wxwork/i.test(navigator.userAgent); //结果为true或者false
    //判断是否为移动端:
    window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i); // true:mobile端, false:PC端

    封装:

    function envjudge() {
      var isMobile = window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i); // 是否手机端
      var isWx = /micromessenger/i.test(navigator.userAgent); // 是否微信
      var isComWx = /wxwork/i.test(navigator.userAgent); // 是否企业微信
    
      if (isComWx && isMobile) { //手机端企业微信
         return 'com-wx-mobile'
       }
      else if (isComWx && !isMobile) { //PC端企业微信
        return 'com-wx-pc'
      }
      else if (isWx && isMobile) { // 手机端微信
         return 'wx-mobile';
      }
      else if (isWx && !isMobile) { // PC端微信
         return 'wx-pc';
      }
      else {
         return 'other';
      }
    
    }
    
    //调用
    
    envjudge()

      

  • 相关阅读:
    Centos7:mariadb替换mysql
    CentOS5 部署 戴尔OMSA
    《Zero MQ》
    可扩展的Web架构和分布式系统
    队列实现
    超级好用的正则表达式网站
    <转>undefined与null的区别
    JS事件
    sublime text 3 快捷键
    设置className的方式(不使用setAttribute)
  • 原文地址:https://www.cnblogs.com/haoran5544/p/16143636.html
Copyright © 2020-2023  润新知