利用useragent判断移动设备

用 User Agent 判断移动设备

WebApp除了做成响应式设计以外,还可以给移动设备做一套UI,PC端再做一套UI,然后在前台进行判断进行相应的跳转。判断是否是移动设备一般根据浏览器的useragent进行判断,虽然可以伪造,但是用起来方便,和Chrome的设备模拟功能配合起来
调试方便。

代码

//根据useragnet判断是否是移动设备
function isMobileDevice(ua) {
    if (/(iphone|ios|android|mini|mobile|mobi|Nokia|Symbian|iPod|iPad|Windowss+Phone|MQQBrowser|wp7|wp8|UCBrowser7|UCWEB|360s+Aphones+Browser)/i.test(ua)) {
        return true;
    }
    return false;
}

$(document).ready(function () {
    var useragent = navigator.userAgent;            
    if (isMobileDevice(useragent)) {
        window.location.href = "/Home/MobileIndex";
        return;
    }
});
原文地址:https://www.cnblogs.com/fanyong/p/3817307.html