判断页面是手机端还是pc端

Page A(PC)
if(手机)跳转PageB

$(function() {
    //判断页面是手机端还是pc端
    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;
            }
        }
        // console.log(userAgentInfo);
        return flag;
    }
    var turl = "";
    //如果是手机端打开
    if (!IsPC()) {
        turl = "/index.php/Mobile/";
        var _str = location.href.split('?')[1];
        if (typeof(_str) != "undefined") {
            turl = turl + "?" + _str;
        }
        window.location.href = turl;
    }
});

Page B(MC)
if(电脑)跳转PageA

$(function() {
    //判断页面是手机端还是pc端
    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;
            }
        }
        // console.log(userAgentInfo);
        return flag;
    }
    var turl = "";
    //如果是手机端打开
    if (IsPC()) {
        turl = "/index.php";
        var _str = location.href.split('?')[1];
        if (typeof(_str) != "undefined") {
            turl = turl + "?" + _str;
        }
        window.location.href = turl;
    }
});

你可能感兴趣的:(前端开发)