前端判断是否手机端网址(双入口解决方案)

2018-07-19  本文已影响20人  小小肉松

userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。

function isMobile () {

    var ua = navigator.userAgent.toLowerCase();

    var StringPhoneReg =  "\\b(ip(hone|od)|android|opera m(ob|in)i"

        + "|windows (phone|ce)|blackberry"

        + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"

        + "|laystation portable)|nokia|fennec|htc[-_]"

        + "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; var StringTableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"

        + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; console.log(ua);

    var isIphone = ua.match(StringPhoneReg),

    isTable = ua.match(StringTableReg),

    isMobile = isIphone || isTable;

    if (isMobile) {

        var lcation = 'http://' + window.location.host + '/m

        window.location.href = lcation;

        return true;

    } else {

        var location = 'http://' + window.location.host

        window.location.href = location;

        return false;

    }

}

最后在<script type="text/javascript"></script>里添加onload事件调用即可

上一篇 下一篇

猜你喜欢

热点阅读