zoukankan      html  css  js  c++  java
  • 根据不同访问设备跳转到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页面的代码:

    <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>
    

      

  • 相关阅读:
    手机访问PC网站自动跳转到手机网站代码(转)
    ecshop其他页面判断是智能手机访问也跳转到ECTouch对应手机版页面(转)
    AJAX
    基于ThinkPHP的开发笔记3-登录功能(转)
    php MySQL使用rand函数随机取记录(转)
    【面试】宝马
    【面试】体会
    【Oracle】ORACLE SQL Developer不支持JAVA版本
    【JavaScript】JS跨域设置和取Cookie
    【Oracle】windows默认共享的打开和关闭?
  • 原文地址:https://www.cnblogs.com/Mr-Tao/p/10544790.html
Copyright © 2011-2022 走看看