根据不同访问设备跳转到PC页面或手机页面

目前很多网站都是采用了响应式自适应页面的设计了,根据访问设备的不同,显示不同的内容。但是还是会有一些节奏比较慢的网站,还是PC页面和手机PAD页面不同的访问域名。正好我这里有个需要,同一个域名要根据不同的访问设备显示PC页面或者手机页面,这里收集两个比较简洁的方法,都是通过JS代码实现的。

第一个:

<script type="text/javascript"> 
    var userAgent = navigator.userAgent.toLowerCase(); 
    var platform; 
    if(userAgent == null || userAgent == ''){
        platform = 'WEB' ;
    }else{
         if(userAgent.indexOf("android") != -1 ){
             platform = 'ANDROID';
             location.href = "http://m.kuegou.com/";
         }else if(userAgent.indexOf("ios") != -1 || userAgent.indexOf("iphone") != -1 || userAgent.indexOf("ipad") != -1){
             platform = 'IOS';
             location.href = "http://m.kuegou.com/";
         }else if(userAgent.indexOf("windows phone") != -1 ){
             platform = 'WP';
             location.href = "http://m.kuegou.com/";
         }else{
             platform = 'WEB' ;
             location.href = "http://www.kuegou.com/";
         }
  }
</script>

直接上代码,修改代码中你的PC页面和手机页面地址即可。

第二个:

这一个是两段代码,分别放到PC页面网页和手机页面网页,实现不同设备访问不同页面都能实现调整,比如电脑访问了手机页面的地址也会跳转到PC页面上来。

首先是放入PC页面的代码:

<script type="text/javascript"> 
    var url = window.location.pathname;
    var wapurl="http://3g.xxx.com"+url;
        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|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
                       window.location.href=wapurl;
                 }else{
                       window.location.href=wapurl;
                 }
            }catch(e){}
    }
}
</script>

下边是放入手机页面的代码:

<script type="text/javascript"> 
     var url = window.location.pathname;
     var pcurl="http://www.xxx.com"+url 
     if(/AppleWebKit.*Mobile/i.test(navigator.userAgent)==false || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))==false){ 
         if(window.location.href.indexOf("?mobile")<0){ 
             try{ 
              if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)==false){ 
                  window.location.href=pcurl;
              } 
         }catch(e){}
      }
    }
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值