Saber 酱的抱枕

Fly me to the moon

03/9
2015
学习

js判断访问网站的是pc还是移动设备

    <script type="text/javascript">
        function browserRedirect() {
            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) {
                alert("phone");
            } else {
                alert("pc");
            }
        }
        browserRedirect();
    </script>

本站底部有个走来走去的saber ,右侧还有个春菜,在手机上看的话,由于屏幕小,感觉挺碍事的,所以找到了js判断移动设备的代码,在移动设备下把这两个地方隐藏了,看起来好了不少。

js判断访问网站的是pc还是移动设备