js判断是电脑或者平板或者手机

<script type="text/javascript"> 
       //第一种方法(推荐使用)
       function isPc(){
            var agents = ["android","windows phone","iphone","ipad","ipod","symbianos"];
            for(var i=0; i<agents.length; i++){
                if(new RegExp(agents[i],"i").test(window.navigator.userAgent)){//获取代理头信息
                    return false;
                }
            }
            return true;
        }
        if(isPc()){
            console.info(window.navigator.userAgent);
            console.info("电脑哦");
        }
        if(!isPc()){
            console.info(window.navigator.userAgent);
            console.info("手机哦");
        }
       
       
        
        //第二种方法
        function IsPC() {
            var userAgentInfo = navigator.userAgent;
            var Agents = ["Android", "iPhone",
                        "SymbianOS", "Windows Phone",
                        "iPad", "iPod"];
            var flag = true;
            for (var v = 0; v < Agents.length; v++) {
                if (userAgentInfo.indexOf(Agents[v]) > 0) {
                    flag = false;
                    break;
                }
            }
            return flag;
        }
        
    </script>


你可能感兴趣的:(判断电脑或者手机)