winterchao
winter-chao
采纳率0%
2017-07-03 06:56 阅读 4.1k

前端判断终端不同访问移动端和PC端页面

公司官网刚开发完,没有考虑移动端的,没有响应式,现在需要做一个移动端的响应式官网;
我需要根据不同的终端选择要不要跳转到移动端官网,这个具体怎么去实现,我前端用js去判断用户代理,然后进行页面跳转吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • showbo GoCityPass新加坡曼谷通票 2017-07-03 07:32

    判断屏幕大小,判断设备如果是ipad那种大屏幕的显示也会很丑,大屏幕的应该显示pc版本的

     <script>if(screen.width>600)location='pc版本的网页';else location='小屏幕网页'</script>
    
    点赞 1 评论 复制链接分享
  • CORA_S_T 千杯不醉476 2017-07-03 07:08

    在onload或者ready函数里加上这段js进行判断转发。

    <script>
    
    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;
    }
    
    var flag = IsPC(); //true为PC端,false为手机端
    alert(flag);
    </script>
    
    

    亲测,可用

    点赞 评论 复制链接分享
  • qq_35235940 听楼一夜雨 2017-07-03 07:14
     function is_mobile() {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
        $is_mobile =  false;//为false 则能在PC上调试 true则不能在PC上调试
        foreach ($mobile_agents as $device) {
            if (stristr($user_agent, $device)) {
                $is_mobile = true;
                break;
            }
        }
        return $is_mobile;
    }
    
    //定义项目名称和路径
    if(is_mobile() ){
        //手机端 
    }else{
       //PC端
    }
    
    点赞 评论 复制链接分享

相关推荐