javaScript ( jquery )判断设备

244 阅读1分钟

下面是代码

javaScript ( jquery )判断设备
判断安卓还是IOS还是其他的,这里有两种方法,
一种是WURFL这是一个库,通过这个库可以看到市面上非常多的设备信息。

<!--
    WURFL
    https://wurfl.io/
=========================================================== -->

<html>

<head>
    <meta charset='utf-8'>
    <title>测试平台</title>
    <script src="https://wurfl.io/wurfl.js"></script>
    <script src="jquery-1.10.2.js"></script>
</head>

<body>
    <ul>
        <li class="wurfl-mobile">IS mobile?:
            <i></i>
        </li>
        <li class="wurfl-form-factor">device:
            <i></i>
        </li>
        <li class="wurfl-device">Browser?:
            <i></i>
        </li>
    </ul>
    <br> -----------------我是华丽分割线-----------------
    <div class="uaJianCe">
    </div>
    <script>
        //第一种,这种是WURFL.js中的方法
        $(".wurfl-mobile i").text(WURFL.is_mobile);
        $(".wurfl-form-factor i").text(WURFL.form_factor);
        $(".wurfl-device i").text(WURFL.complete_device_name);
        console.log(WURFL)
        // if (WURFL.is_mobile) {
        //     //document.write('Mobile<br>');
        // }

        // if (!WURFL.is_mobile) {
        //     //   document.write('Not Mobile<br>');
        // }

        // if (WURFL.form_factor == 'Desktop') {
        //     //  document.write('Desktop<br>');
        // }


        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 (bIsIphoneOs) {
            $(".uaJianCe").html("你是一部IOS手机")
        } else if (bIsAndroid) {
            $(".uaJianCe").html("你是一部安卓手机")
        } else {
            //如果是电脑就不动
            $(".uaJianCe").html("你可能是其他的设备")
        }

    </script>
</body>

</html>