For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /note/js/device-detect.md.

设备环境判断

检测 WeChat、Android、iOS 环境

代码示例
// 检测微信状态下的手机设备环境
function checkHJByWeChat() {
    // 设备变量区分
    let web = navigator.userAgent,
        isiOS = !!web.match(/\i[^;]+;(U;)? CPU.+Mac OS X/),
        isAndroid = web.indexOf("Android") > -1 || web.indexOf("Adr") > -1,
        isWeChat =
            web.toLocaleLowerCase().match(/MicroMessenger/i) ==
            "micromessenger";

    let test = "";
    // WeChat 环境下
    if (isWeChat) {
        console.log("当前环境为 WeChat");
        if (isAndroid) {
            // Android 环境下
            test = "当前环境为 WeChat--> Android";
        } else if (isiOS) {
            // iOS 环境下
            test = "当前环境为 WeChat--> iOS";
        } else {
            test = "当前环境为 WeChat-->";
        }
    } else {
        // 非 WeChat 环境
        console.log("当前环境为 非WeChat");
        if (isAndroid) {
            test = "当前环境为 非WeChat--> Android";
        } else if (isiOS) {
            test = "当前环境为 非WeChat--> iOS";
        } else {
            test = "当前环境为 非WeChat-->";
        }
    }
    console.log(test);
    document.querySelector("#test").innerHTML = test;
}

浏览器手机环境检测

代码示例
function checkHJ() {
    let web = navigator.userAgent;

    let isWeChat =
        web.toLocaleLowerCase().match(/MicroMessenger/i) == "micromessenger";
    // WeChat
    if (isWeChat) {
        console.log("当前环境为 WeChat");
    }

    let isAndroid = web.indexOf("Android") > -1 || web.indexOf("Adr") > -1;
    // Android
    if (isAndroid) {
        console.log("当前环境为 Android");
    }

    let isiOS = !!web.match(/\i[^;]+;(U;)? CPU.+Mac OS X/);
    // iOS
    if (isiOS) {
        console.log("当前环境为 iOS");
    }

    // others 浏览器
    console.log("当前环境为 浏览器");
}