您好,欢迎来到百家汽车网。
搜索
您的当前位置:首页如何用js判断设备类型?

如何用js判断设备类型?

来源:百家汽车网

userAgent是HTTP请求中的用户标识,是能够代表客户端类型的字符串,比如浏览器类型 操作系统等信息。

console.log(navigator)
console.log(navigator.userAgent);

 判断设备是PC端还是移动端: false PC端 | true 移动端

//判断设备是PC端还是移动端: false PC端 | true 移动端
const isMobile = () => {
    return !!navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|WebOS|Windows Phone|Phone)/i);
}

判断设备时安卓还是IOS

// 判断设备时安卓还是IOS
const isAndroid = () => {
    return /android/i.test(navigator.userAgent.toLowerCase());
};
const isIOS = () => {
    let reg = /iPhone|iPad|iOS|Macintosh/i;
    return reg.test(navigator.userAgent.toLowerCase());
};

判断浏览器类型及其版本

// 获取浏览器类型及其版本
        const getExplorerInfo = () => {
            let t = navigator.userAgent.toLowerCase();
            return 0 <= t.indexOf("mise")
                ? {
                    // IE<11
                    type: "IE",
                    version: Number(t.match(/mise ([\d]+)/)[1]),
                }
                : !!t.match(/trident\/.+?rv:(([\d.]+))/)
                    ? {
                        // IE 11
                        type: "IE",
                        version: 11,
                    }
                    : 0 <= t.indexOf("edge")
                        ? {
                            type: "Edge",
                            version: Number(t.match(/edge\/([\d]+)/)[1]),
                        }
                        : 0 <= t.indexOf("firefox")
                            ? {
                                type: "Firefox",
                                version: Number(t.match(/firefox\/([\d]+)/)[1]),
                            }
                            : 0 <= t.indexOf("chrome")
                                ? {
                                    type: "Chrome",
                                    version: Number(t.match(/chrome\/([\d/]+)/)[1]),
                                }
                                : 0 <= t.indexOf("opera")
                                    ? {
                                        type: "Safari",
                                        version: Number(t.match(/version\/([\d]+)/)[1]),
                                    }
                                    : {
                                        type: t,
                                        version: -1,
                                    };
        }

测试:打开控制台(检查自己的浏览器)

console.log(isMobile());//false电脑
        if (isMobile() == false) {
            alert("PC端");
            alert("浏览器类型:" + getExplorerInfo().type + "," + "版本号:" + getExplorerInfo().version)
            console.log("PC端", getExplorerInfo());//浏览器类型
        } else {
            alert("移动端")
            console.log("移动端");
            if (isAndroid() !== false) {
                alert("安卓")
                console.log("安卓");
            } else {
                console.log("不是安卓");
            }
            if (isIOS() !== false) {
                alert("IOS")
                console.log("IOS");
            } else {
                console.log("不是IOS");
            }
        }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baijiahaobaidu.com 版权所有 湘ICP备2023023988号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务