判断是否是手机端并跳转

本文提供了多种用于检测用户访问设备类型并实现相应跳转的JavaScript代码实例,包括针对不同移动设备如iPad、iPhone和Android的特定跳转逻辑。

转载于:http://www.xker.com/page/e2014/0325/131654.html

常用跳转代码

以下代码为常用来判断访问者的访问设备的类型来进行相应的跳转

<script type="text/javascript">
    // borwserRedirect
    (function browserRedirect(){
      var sUserAgent = navigator.userAgent.toLowerCase();
      var bIsIpad = sUserAgent.match(/ipad/i) == 'ipad';
      var bIsIphone = 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) == 'web';
      var bIsCE = sUserAgent.match(/windows ce/i) == 'windows ce';
      var bIsWM = sUserAgent.match(/windows mobile/i) == 'windows mobile';
      var bIsAndroid = sUserAgent.match(/android/i) == 'android';

      if(bIsIpad || bIsIphone || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM || bIsAndroid ){
        window.location.href = '跳转的移动端网址';
      }
    })();
 </script>
<script type="text/javascript"> 
<!-- 
        //平台、设备和操作系统 
        var system = { 
            win: false, 
            mac: false, 
            xll: false, 
            ipad:false 
        }; 
        //检测平台 
        var p = navigator.platform; 
        system.win = p.indexOf("Win") == 0; 
        system.mac = p.indexOf("Mac") == 0; 
        system.x11 = (p == "X11") || (p.indexOf("Linux") == 0); 
        system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false; 
        //跳转语句,如果是手机访问就自动跳转到wap.baidu.com页面 
        if (system.win || system.mac || system.xll||system.ipad) { 

        } else { 

            window.location.href = "http://www.jdpatro.com/3g/"; 
        } 
--> 
</script> 
<script>
(function(){if(/iphone|nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){document.write('<a href="检测为移动设备后跳转的移动端网址" style="color:#000;">手机触屏版</a>')}})();
</script>

腾讯跳转

<script type="text/javascript">
if(window.location.toString().indexOf('pref=padindex') != -1){
}else{
    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){  
      if(window.location.href.indexOf("?mobile")<0){
        try{
            if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
                window.location.href="http://xw.qq.com/index.htm";
            }else if(/iPad/i.test(navigator.userAgent)){
              //window.location.href="http://www.qq.com/pad/"
            }else{
                window.location.href="http://xw.qq.com/simple/s/index/"
            }
        }catch(e){}
    }
    }
}
</script>
<script type="text/javascript"> 
<!-- 
        //平台、设备和操作系统 
        var system = { 
            win: false, 
            mac: false, 
            xll: false, 
            ipad:false 
        }; 
        //检测平台 
        var p = navigator.platform; 
        system.win = p.indexOf("Win") == 0; 
        system.mac = p.indexOf("Mac") == 0; 
        system.x11 = (p == "X11") || (p.indexOf("Linux") == 0); 
        system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false; 
        //跳转语句,如果是手机访问就自动跳转到wap.baidu.com页面 
        if (system.win || system.mac || system.xll||system.ipad) { 

        } else { 

            window.location.href = "http://www.jdpatro.com/3g/"; 
        } 
--> 
</script> 

JS 判断浏览器客户端类型(ipad,iphone,android)

<script type="text/javascript">  
   var bForcepc = fGetQuery("dv") == "pc";   
   function fBrowserRedirect(){   
       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){   
           var sUrl = location.href;      
           if(!bForcepc){   
               window.location.href = "http://ipad.mail.163.com/";  
           }   
       }   
       if(bIsIphoneOs || bIsAndroid){   
           var sUrl = location.href;      
           if(!bForcepc){   
               window.location.href = "http://smart.mail.163.com/";  
           }   
       }   
       if(bIsMidp||bIsUc7||bIsUc||bIsCE||bIsWM){   
           var sUrl = location.href;      
           if(!bForcepc){   
               window.location.href = "http://m.mail.163.com/";  
           }   
       }   
   }  
   function fGetQuery(name){//获取参数值   
       var sUrl = window.location.search.substr(1);  
       var r = sUrl.match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));  
       return (r == null ? null : (r[2]));  
   }  
   function fShowVerBlock(){     
       if(bForcepc){   
           document.getElementByIdx_x("dv_block").style.display = "block";  
       }   
       else{   
           document.getElementByIdx_x("ad_block").style.display = "block";  
       }   
   }  
   fBrowserRedirect();   
   </script> 

使用插件判断

我们可以使用device.js来判断打开网页的类型,然后进行跳转

<script src="device.min.js"></script>
<script>
    if(device.windows()==true){    /*PC端*/

    }else if(device.ipad()==true){   /*pad端*/
        window.location.href = 'pad.html';
    }else if(device.iphone()==true){    /*手机端*/
        window.location.href = 'phone.html';
    }
    </script>
<think>好的,我现在需要帮用户解决如何判断用户设备是否为手机端的问题。用户之前提到了使用Vue、Nginx以及JavaScript的方法,所以我得综合这些信息给出一个全面的回答。 首先,用户可能是在开发一个需要区分PC和移动端的网页应用,可能是为了不同的布局或功能。根据引用内容,常见的方法是通过检测navigator.userAgent字符串。用户提到了Vue中的_isMobile方法,使用正则表达式匹配设备类型,这应该是一个前端的解决方案。 然后,引用中还提到了Nginx的配置,通过判断User-Agent来设置不同的root路径,这属于服务器端的处理。用户可能需要知道前端和后端的不同实现方式,所以我需要分点说明这两种方法。 另外,用户可能需要考虑不同场景下的选择,比如是否需要服务端渲染,或者是否希望减少客户端计算。这时候比较两种方法的优缺点就很重要了。比如前端检测简单但可能被篡改,后端检测更可靠但需要服务器配置。 还要注意正则表达式的准确性,确保覆盖大部分移动设备的关键字,比如iPhone、Android等。可能需要提供一个更全面的正则表达式,或者建议使用现有的库来提升准确性和维护性。 用户可能还会关心移动端适配的其他问题,比如响应式设计,但根据问题描述,用户明确想要根据设备类型跳转不同页面,所以应该专注于检测方法而不是自适应布局。 最后,生成的相关问题需要覆盖常见的后续疑问,比如优缺点、其他检测方法、正则表达式优化以及如何实现自动跳转。这些能帮助用户进一步深入学习和解决问题。</think>### 如何判断用户设备是否为手机端? #### 一、核心方法:`User-Agent`检测 所有现代设备浏览器都会在请求头中携带`navigator.userAgent`字符串,通过解析该字符串可判断设备类型。以下是具体实现方式: 1. **JavaScript前端检测** ```javascript function isMobile() { return /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|Windows Phone)/i .test(navigator.userAgent); } ``` - **使用场景**:Vue/React等前端框架的页面跳转逻辑控制[^3] - **触发时机**:建议在`mounted()`或路由守卫中调用 2. **Nginx服务端检测** ```nginx server { set $is_mobile 0; if ($http_user_agent ~* (mobile|android|iphone|ipad|windows\s+phone)) { set $is_mobile 1; } location / { root $is_mobile ? /mobile/path : /pc/path; } } ``` - **优势**:减少客户端计算,避免JS被禁用的情况[^2] - **应用场景**:静态资源分流、域名跳转 #### 二、进阶优化方案 1. **动态视口适配** 结合CSS媒体查询提升准确性: ```css @media (pointer: coarse) and (hover: none) { /* 触摸设备专属样式 */ } ``` 2. **混合检测策略** ```javascript function enhancedIsMobile() { return isMobile() || window.matchMedia("(max-width: 768px)").matches || ('ontouchstart' in window); } ``` #### 三、方法对比 | 检测方式 | 准确性 | 可靠性 | 实现成本 | 适用场景 | |----------------|--------|--------|----------|------------------| | 纯前端检测 | 中 | 中 | 低 | SPA应用 | | 服务端检测 | 高 | 高 | 中 | 多端独立项目 | | 混合检测 | 高 | 高 | 高 | 高精度要求场景 | #### 四、注意事项 1. 部分平板设备(如iPad Pro)可能被识别为PC端 2. Chrome开发者工具的模拟器可能绕过基础检测 3. 建议定期更新正则表达式中的设备关键词
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值