JavaScript 判断企业微信与微信环境的方法

作者:木棉

时间:2026-10-31

浏览:315

本文介绍如何使用 JavaScript 通过 UserAgent 字符串区分企业微信、普通微信及非微信环境。提供了封装好的判断函数及多种检测代码片段,帮助开发者根据当前客户端类型执行相应的认证或跳转逻辑。

文章目录

前言:

介绍如何利用 JavaScript 代码来识别当前的运行环境,具体区分是企业微信还是普通微信。

封装方法:

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'
  }
}

使用方法:

const envResult = envjudge()
// 企业微信
if (envResult === 'com-wx-mobile' || envResult === 'com-wx-pc') {
}

附:判断是否是微信内置浏览器,并区分企业微信和微信。

if(/MicroMessenger/i.test(navigator.userAgent)){
    alert("微信浏览器");
}else{
    alert("请使用微信浏览器打开");
}
        var ua= window.navigator.userAgent.toLowerCase();
        if( (ua.match(/MicroMessenger/i) == 'micromessenger') && (ua.match(/wxwork/i) == 'wxwork') ){
            console.log("企业微信客户端");
        }else if( ua.match(/micromessenger/i) == 'micromessenger' ){
            console.log("微信客户端");
        }

在实际开发中,经常需要判断页面是否由微信浏览器打开。如果是,则执行微信认证或跳转操作。

如果不是微信环境,则不进行跳转,或者采用其他认证方式。

总结

到此这篇关于如何用js判断当前是否是企业微信环境还是微信环境的文章就介绍到这了,更多相关js判断当前微信环境内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章