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

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

作者: 小小肉松 | 来源:发表于2018-07-19 14:51 被阅读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事件调用即可

相关文章

网友评论

      本文标题:前端判断是否手机端网址(双入口解决方案)

      本文链接:https://www.haomeiwen.com/subject/cqjtmftx.html