JS获取移动端系统信息(操作系统、操作系统版本、横竖屏状态、设备类型、网络状态、生成浏览器指纹)

本文介绍了一种使用JavaScript来检测设备的操作系统、版本、屏幕方向、类型、网络状态及生成浏览器指纹的方法。通过分析用户代理字符串,可以识别出Android、iOS、Windows Phone等操作系统,并确定设备是否为手机、iPad或PC。此外,还提供了获取网络连接类型(如WiFi、4G、3G)和屏幕方向(横屏或竖屏)的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function getOS() { // 获取当前操作系统
    var os;
    if (navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1) {
        os = 'Android';
    } else if (navigator.userAgent.indexOf('iPhone') > -1) {
        os = 'iOS';
    } else if (navigator.userAgent.indexOf('Windows Phone') > -1) {
        os = 'WP';
    } else {
        os = 'Others';
    }
    return os;
}

function getOSVersion() { // 获取操作系统版本
    var OSVision = '1.0';
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //Android
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) {
        OSVision = navigator.userAgent.split(';')[1].match(/\d+\.\d+/g)[0];
    }
    if (isIOS) {
        OSVision = navigator.userAgent.split(';')[1].match(/(\d+)_(\d+)_?(\d+)?/)[0];
    }
    return OSVision;
}

function getOrientationStatu() { // 获取横竖屏状态
    var orientationStatu;
    if (window.screen.orientation.angle == 180 || window.screen.orientation.angle == 0) { // 竖屏
        orientationStatu = '竖屏';
    }
    if (window.screen.orientation.angle == 90 || window.screen.orientation.angle == -90) { // 横屏
        orientationStatu = '横屏';
    }
    return orientationStatu;
}

function getDeviceType() { // 获取设备类型
    var deviceType;
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/(ipad)/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

    if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) {
        deviceType = 'PC'; //pc
    } else if (bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        deviceType = 'phone'; //phone
    } else if (bIsIpad) {
        deviceType = 'ipad'; //ipad
    } else {
        deviceType = '未知';
    }
    return deviceType;
}

function getNetWork() { // 获取网络状态
    var netWork;
    switch (navigator.connection.effectiveType) {
        case 'wifi':
            netWork = 'wifi'; // wifi
            break;
        case '4g':
            netWork = '4G'; // 4g
            break;
        case '2g':
            netWork = '2G'; // 2g
            break;
        case  '3g':
            netWork = '3G'; // 3g
            break;
        case  'ethernet':
            netWork = '以太网'; // ethernet
            break;
        case  'default':
            netWork = '未知'; // 未知
            break;
    }
    return netWork;
}

function createFingerprint(domain) { // 生成浏览器指纹
    var fingerprint;

    function bin2hex(s) {
        var i, l, n, o = '';
        s += '';
        for (i = 0, l = s.length; i < l; i++) {
            n = s.charCodeAt(i)
                .toString(16);
            o += n.length < 2 ? '0' + n : n;
        }
        return o;
    }

    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    var txt = domain || window.location.host;
    ctx.textBaseline = "top";
    ctx.font = "14px 'Arial'";
    ctx.textBaseline = "tencent";
    ctx.fillStyle = "#f60";
    ctx.fillRect(125, 1, 62, 20);
    ctx.fillStyle = "#069";
    ctx.fillText(txt, 2, 15);
    ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
    ctx.fillText(txt, 4, 17);

    var b64 = canvas.toDataURL().replace("data:image/png;base64,", "");
    var bin = atob(b64);
    var crc = bin2hex(bin.slice(-16, -12));
    fingerprint = crc;
    return fingerprint;

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值